15% off all plans & roadmaps Use code
See plansProblem editorial
Implement a class named MyHashSet that stores a collection of integer keys. A key can appear at most once, and the set initially contains no keys. Built-in hash set or hash table libraries must not be used.
Design HashSet is a Array/Matrix Manipulation Patterns problem, in the Hashing - Previously Seen / Existence Check subpattern. The pattern covers in-place operations, hashing, prefix sums, spiral traversal, and cyclic sort
The reference solution runs in O(1) per add, remove, or contains operation time and O(10^6) space.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Design HashSet is a Array/Matrix Manipulation Patterns problem, in the Hashing - Previously Seen / Existence Check subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.