This is not O(n) because you're iterating over "seen" at every iteration of your for loop, so it's O(n^2) except in the degenerate case where all the elements are duplicates. Also, it isn't specified but unshift itself is also most likely an O(n) operation in most implementations.
This is not O(n) because you're iterating over "seen" at every iteration of your for loop, so it's O(n^2) except in the degenerate case where all the elements are duplicates. Also, it isn't specified but unshift itself is also most likely an O(n) operation in most implementations.
I had my doubts, but it seems the JS implementation uses a linear search algorithm.
FWIW, I also had to check if Sets keep insertion ordering since in principle they shouldn't... but in JS they do!
Good to know, I didn't know that was actual documented behavior of the Set.