collapseDuplicates complete with a main to test it out.