solution to removeDuplicates2