Contains Duplicate

Check if any value appears at least twice in the array.

Pattern

Hash Set

This problem follows the Hash Set pattern, commonly found in the Arrays & Hashing category. Recognizing this pattern is key to solving it efficiently in an interview setting.

Approach

How to Solve It

Add each element to a set. If it already exists, return true.

Key Insight

A hash set gives O(1) lookup — checking 'have I seen this before?' is the core operation.

Step-by-step

  1. 1Create an empty hash set
  2. 2Iterate through each number in the array
  3. 3If the number is already in the set, return true
  4. 4Otherwise, add the number to the set
  5. 5If loop completes without finding a duplicate, return false

Pseudocode

seen = new Set()
for num in nums:
    if num in seen:
        return true
    seen.add(num)
return false
Complexity Analysis

Time Complexity

O(n)

Space Complexity

O(n)
More Arrays & Hashing Problems

Master this pattern with YeetCode

Practice Contains Duplicate and similar Arrays & Hashing problems with flashcards. Build pattern recognition through active recall.

Practice this problem