Suppose I have a list like this:
[ 2, 7, 2, 3, 1, 1, 4, 5, 3, 6, 4 ]
And I want to sort and remove duplicates to yield:
[ 1, 2, 3, 4, 5, 6, 7 ]
I can achieve this by removing duplicates and then sorting:
const uniqueAndSorted = xs => [ ...new Set(xs) ].sort();
However, this seems inefficient, since I could probably detect duplicates as I do the sorting.
What is the optimal way to sort and remove duplicates from a list?
(JavaScript implementations are preferred; the function should be non-destructive)