Here's my python solution O(n). The idea is to iterate over the input and keep track of the index of the last time you saw a given number. If you see a number, replace the old value with None, and do a second pass at the end to remove all the Nones.
The two-phase approach is necessary to keep it O(n). If we remove items as we find them it could end up costing O(n^2).
Here's my python solution
O(n)
. The idea is to iterate over the input and keep track of the index of the last time you saw a given number. If you see a number, replace the old value withNone
, and do a second pass at the end to remove all theNone
s.The two-phase approach is necessary to keep it
O(n)
. If we remove items as we find them it could end up costingO(n^2)
.