Sort the elements of $S$, which takes $O(n\log n)$ time. Then, step through the sequence looking for two consecutive elements that are equal, which takes an additional $O(n)$ time. Overall, this method takes $O(n\log n)$ time.