I believe this solution is O (N) both in time and in space:
HashSet seenOnce;
HashSet seenMultiple;
for each in input
if item in seenMultiple
next
if item in seenOnce
remove item from seenOnce
add to item seenMultiple
else
add to item seeOnce
smallest = SENTINEL
for each in seenOnce
if item < smallest
smallest = item
If you have a limited range of integral values, you can replace HashSets with BitArrays indexed by value.
source
share