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
- Is the question only about presence, not count?
- Do you need uniqueness, intersection, or visited state?
- 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: true — 1 appears at index 0 and again at index 5, caught after 5 additions to the set.
Common pitfalls
Storing mutable objects when value equality is expected.
Waiting too long to mark
visitedin graph-style problems.