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
DifficultyPatternProblemKey Insight
EasymembershipLC 217 ยท Contains DuplicateIf inserting into the set ever fails, you found a duplicate. That turns repeated linear checks into O(1) membership tests.
Easyset-opsLC 349 ยท Intersection of Two ArraysBuild a set from one array, then keep only values that appear in the other. The set automatically deduplicates the answer.
EasycycleLC 202 ยท Happy NumberRepeated states imply a cycle. Store every intermediate value in a set; if a value repeats before reaching 1, the number is not happy.
MediumsequenceLC 128 ยท Longest Consecutive SequenceOnly start expanding from numbers with no predecessor. The HashSet makes both predecessor and successor checks O(1).
MediumvisitedLC 127 ยท Word LadderA set-backed dictionary enables O(1) word existence checks, and a visited set prevents revisiting words during BFS.
Mediumconstraint-checkLC 36 ยท Valid SudokuTrack 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.

โ† Back to Arrays & Hashing problems