196. Design HashSet
Easy · Design
Design a HashSet without using any built-in hash table libraries. Implement the following operations on the HashSet: `add(key)` — inserts the value `key` into the HashSet, `contains(key)` — returns whether the value `key` exists in the HashSet, and `remove(key)` — removes the value `key` from the HashSet (if it does not exist, do nothing).
You are given a list of operations and their arguments. The first operation is always `"HashSet"` (constructor, no argument). Execute each operation in order and collect the return values. The constructor returns `null`, `add` returns `null`, `remove` returns `null`, and `contains` returns `true` or `false`.
All keys are non-negative integers.
Examples
Example 1 Input: operations = ["HashSet","add","add","contains","contains","add","contains","remove","contains"], args = [[],[1],[2],[1],[3],[2],[2],[2],[2]] Output: [null,null,null,true,false,null,true,null,false] Explanation: After adding 1 and 2, contains(1)=true, contains(3)=false. After adding 2 again (no-op), contains(2)=true. After removing 2, contains(2)=false.
Example 2 Input: operations = ["HashSet","add","remove","contains"], args = [[],[5],[5],[5]] Output: [null,null,null,false] Explanation: Add 5, then remove 5, then contains(5) returns false.
Constraints
- Standard input/output constraints apply