Hashing & Sets
Set Membership
🧿
Picture this
A gate guard checks each token against a short forbidden list and waves through only unseen visitors.
When you see it
Signal words
If the prompt uses any of these, this pattern should come to mind first.
uniqueduplicatecontainsvisitedmembershipintersection
Ask yourself
- Is the question only about presence, not count?
- Do you need uniqueness, intersection, or visited state?
- Can membership checks be O(1)?
Canonical template
const seen = new Set<string>();
for (const item of items) {
if (seen.has(item)) return true;
seen.add(item);
}
return false;
Common pitfalls
Storing mutable objects when value equality is expected.
Waiting too long to mark
visitedin graph-style problems.