AlgoLounge

Design HashSet

easy

LeetCode 705. Design HashSet

Design a HashSet without using any built-in hash table libraries. You may not use set() or dict().

Implement the MyHashSet class:

  • MyHashSet() initializes the object with an empty set.
  • add(key) Inserts the value key into the HashSet.
  • contains(key) Returns whether the value key exists in the HashSet or not.
  • remove(key) Removes the value key in the HashSet. If key does not exist in the HashSet, do nothing.

Examples

Example 1

Input:  ["MyHashSet()", "add(1)", "add(2)", "contains(1)", "contains(3)", "add(2)", "contains(2)", "remove(2)", "contains(2)"]
Output: [null, null, null, true, false, null, true, null, false]

MyHashSet myHashSet = new MyHashSet(); myHashSet.add(1); // set = [1] myHashSet.add(2); // set = [1, 2] myHashSet.contains(1); // return true myHashSet.contains(3); // return false (not found) myHashSet.add(2); // set = [1, 2] myHashSet.contains(2); // return true myHashSet.remove(2); // set = [1] myHashSet.contains(2); // return false (already removed)

Constraints

  • 0 <= key <= 1,000,000
  • At most 10,000 calls will be made to add, remove, and contains.

How the tests work: each test is a sequence of calls. operations[i] is the method called with arguments[i]; the first entry constructs the class. The expected output lists what every call returns — null for the constructor and for methods that return nothing.

TypeScript
Loading editor…
List of test cases (10)
Case 1
operations = ["MyHashSet","add","add","contains","contains","add","contains","remove","contains"]
arguments = [[],[1],[2],[1],[3],[2],[2],[2],[2]]
Case 2
operations = ["MyHashSet","add","remove","contains"]
arguments = [[],[1],[1],[1]]
Case 3
operations = ["MyHashSet","contains","add","contains","remove","contains"]
arguments = [[],[10],[10],[10],[10],[10]]
Case 4
operations = ["MyHashSet","add","add","add","contains","contains","contains"]
arguments = [[],[0],[1000000],[500],[0],[1000000],[999]]
Case 5
operations = ["MyHashSet","add","add","remove","remove","contains","contains"]
arguments = [[],[1],[1],[1],[2],[1],[2]]
Case 6
operations = ["MyHashSet","add","add","add","add","add","add","add","add","add","add"]
arguments = [[],[1],[2],[3],[4],[5],[6],[7],[8],[9],[10]]
Case 7
operations = ["MyHashSet","add","contains","add","contains"]
arguments = [[],[1],[1],[1001],[1001]]
Case 8
operations = ["MyHashSet","remove","contains"]
arguments = [[],[14],[14]]
Case 9
operations = ["MyHashSet","add","add","contains","remove","contains"]
arguments = [[],[1],[1],[1],[1],[1]]
Case 10
operations = ["MyHashSet","add","add","add","contains","contains","contains","remove","contains"]
arguments = [[],[5],[1005],[2005],[5],[1005],[2005],[1005],[1005]]