Optimized algorithm with SortedSet

Sets are like Lists, but all their items are guaranteed to be unique.  By choosing TreeMap (which implements SortedSet), we can also guarantee its items are sorted.  Still not the most efficient algorithm, but I thought it was neat.