← All patterns
28 / 32
Hashing & Sets

Set Membership

When you see it

Signal words

If the prompt uses any of these, this pattern should come to mind first.

uniqueduplicatecontainsvisitedmembershipintersection
Practice

Drill this pattern with an AI trainer — reply only with Hint, Next, Lost, or paste your code.

Ask yourself

  1. Is the question only about presence, not count?
  2. Do you need uniqueness, intersection, or visited state?
  3. Can membership checks be O(1)?

Classic problem

Contains Duplicate (LeetCode 217) — return true if any value in nums appears more than once. Track what you’ve seen; bail the moment a value repeats.

Input: nums = [1, 2, 3, 4, 5, 1]

Output: true1 appears at index 0 and again at index 5, caught after 5 additions to the set.

Watch it run

Common pitfalls

Storing mutable objects when value equality is expected.

Waiting too long to mark visited in graph-style problems.