LeetCode ยท HashSet
HashSet Problem Set
Membership, deduplication, cycle detection, visited tracking, and set-based constraint checks. If you keep asking โhave I seen this before?โ a HashSet is probably the right tool.
Related pages: HashSet ยท
Arrays & Hashing
| Difficulty | Pattern | Problem | Key Insight |
|---|---|---|---|
| Easy | membership | LC 217 ยท Contains Duplicate | If inserting into the set ever fails, you found a duplicate. That turns repeated linear checks into O(1) membership tests. |
| Easy | set-ops | LC 349 ยท Intersection of Two Arrays | Build a set from one array, then keep only values that appear in the other. The set automatically deduplicates the answer. |
| Easy | cycle | LC 202 ยท Happy Number | Repeated states imply a cycle. Store every intermediate value in a set; if a value repeats before reaching 1, the number is not happy. |
| Medium | sequence | LC 128 ยท Longest Consecutive Sequence | Only start expanding from numbers with no predecessor. The HashSet makes both predecessor and successor checks O(1). |
| Medium | visited | LC 127 ยท Word Ladder | A set-backed dictionary enables O(1) word existence checks, and a visited set prevents revisiting words during BFS. |
| Medium | constraint-check | LC 36 ยท Valid Sudoku | Track digits already seen in each row, column, and box. Any repeat means the board violates the Sudoku constraints. |
Practice note: Solution write-ups already exist for LC 217, 128, 127, and 36. LC 349 and LC 202 are included as canonical HashSet practice problems even though their dedicated solution pages have not been added yet.