wrote collapseDuplicates()