Design HashSet

EasyArrayHash TableSQL

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. Return the result as an array.

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:

Add 1 and 2, check whether 1 and 3 are present, add 2 again, check 2, remove 2, then check 2 again. The exact output is [null,null,true,false,null,true,null,false].

Input:operations = [[1]]
Output:[1]
Explanation:

The single operation produces the exact output [1].

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:

The exact output is [null, true, null, false, null, null, null, true, true]: after adding 5, contains(5) is true, removing 5 succeeds, the next contains(5) is false, removing 5 again has no effect, then adding 10 and 5 back makes both final contains checks true.

Constraints

  • 0 ≤ key ≤ 10⁶

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!