#217

Contains Duplicate

Easy HashSet

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Return true if any value appears at least twice in the array.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: does any value repeat?

Why this works: add to a set; a failed add means a repeat.

Example: [1, 2, 3, 1]

add 1, 2, 3 ✓, add 1 ✗ → true

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Using a List and contains() — that's O(n) per check, O(n²) overall.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(n).
Topic: Hashing
🎬 Episodes 37–39: video coming soon