Find Duplicate

Find the first duplicate element that appears in an array using a hash set for efficient lookup.

Code

Algorithms
seen = set()
result = None
for x in arr:
    if x in seen:
        result = x
        break
    seen.add(x)
result

Parameters

Array to check

Server

More Python Snippets