On Apr 5, 8:51am, Bart Schaefer wrote: } } Or perhaps someone else has an even more clever idea. Anything better } than O(N^2) would help. Incidentally the requirement that the output remain in the same order as the input puts a dent in many of the algorithms you'll find with google on this question.