Skip to content

visual walkthrough

Contains Duplicate

EasyHash SetReported at: AmazonGoogleApple+5

Shows the time-vs-space trade-off between sorting and hashing in its simplest form.

Solve on LeetCode

The idea

A duplicate exists the moment you meet a number you've already met. A set answers "have I seen this?" in one step, so a single pass is enough.

Comparing every pair does the same job the slow way.

Complexity

approachtimespace
Compare every pairO(n²)O(1)
Hash setO(n)O(n)

More walkthroughs