Description
Design a HashSet without using any built-in hash table libraries. Implement the MyHashSet class with add(key), remove(key), and contains(key) methods that operate on non-negative integers.
Examples
Input:
operations = [["add",1],["add",2],["contains",1],["contains",3],["add",2],["contains",2],["remove",2],["contains",2]]Output:
[null,null,true,false,null,true,null,false]Explanation:
HashSet operations.
Input:
operations = [[1]]Output:
[1]Explanation:
A single HashSet operation produces one result.
Input:
operations = [["add",5],["contains",5],["remove",5],["contains",5],["remove",5],["add",10],["add",5],["contains",10],["contains",5]]Output:
[null,true,null,false,null,null,null,true,true]Explanation:
Add 5 to empty set, check if 5 exists (true), remove 5, check if 5 exists (false), try removing 5 again (no effect), add 10, add 5 back, then check both 10 and 5 exist (both true). This demonstrates removing non-existent elements and re-adding previously removed elements.
Constraints
- •
0 ≤ key ≤ 10⁶