DEV Community

Discussion on: Algorithm Problem: First Duplicate in Array

Collapse
 
jdsteinhauser profile image
Jason Steinhauser

You can push elements into a Set and check to see if it exists already when while iterating. If it exists, then you can terminate and return the index of the repeated element. This should run in O(n) time (disregarding Set resizing).