Use sorted set to eliminate duplicates in input