There's also a linear time and constant space algorithm based on partitioning, which may be more flexible if you're trying to apply this to variants of the problem that the mathematical approach doesn't work well on. This requires mutating the underlying array and has worse constant factors than the mathematical approach. More specifically, I believe the costs in terms of the total number of values and the number of duplicates are and respectively, though proving it rigorously will take more time than I have at the moment.
Start with a list of pairs, where the first pair is the range over the whole array, or if 1-indexed.
Repeat the following steps until the list is empty:
- Take and remove any pair from the list.
- Find the minimum and maximum, and , of the denoted subarray.
- If , the subarray consists only of equal elements. Yield its elements except one and skip steps 4 to 6.
- If , the subarray contains no duplicates. Skip steps 5 and 6.
- Partition the subarray around , such that elements up to some index are smaller than the separator and elements above that index are not.
- Add and to the list.
Cursory analysis of time complexity.
Steps 1 to 6 take time, since finding the minimum and maximum and partitioning can be done in linear time.
Every pair in the list is either the first pair, , or a child of some pair for which the corresponding subarray contains a duplicate element. There are at most such parents, since each traversal halves the range in which a duplicate can be, so there are at most total when including pairs over subarrays with no duplicates. At any one time, the size of the list is no more than .
Consider the work to find any one duplicate. This consists of a sequence of pairs over an exponentially decreasing range, so the total work is the sum of the geometric sequence, or . This produces an obvious corollary that the total work for duplicates must be , which is linear in .
To find a tighter bound, consider the worst-case scenario of maximally spread out duplicates. Intuitively, the search takes two phases, one where the full array is being traversed each time, in progressively smaller parts, and one where the parts are smaller than so only parts of the array are traversed. The first phase can only be deep, so has cost , and the second phase has cost because the total area being searched is again exponentially decreasing.