Make removeDuplicates() more memory efficient