Design HashSet

IF
AlgoAxiomStaff Engineers
JSTS
Easy20 mins

Design a HashSet without using any built-in hash table libraries.

Implement a function that processes an array of operations and an array of arguments:

  • "MyHashSet" — Initializes the HashSet object.
  • "add" — Inserts the value key into the HashSet.
  • "remove" — Removes the value key from the HashSet. If key does not exist, do nothing.
  • "contains" — Returns true if the value key exists in the HashSet, false otherwise.

Examples

Example 1:

Input: operations = ["MyHashSet","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: Add 1 and 2. Contains(1) is true. Contains(3) is false. Add 2 again (no-op). Contains(2) is true. Remove 2. Contains(2) is false.

Example 2:

Input: operations = ["MyHashSet","add","contains","remove","contains"], args = [[],[100],[100],[100],[100]]

Output: [null,null,true,null,false]

Explanation: Add 100, confirm it exists, remove it, confirm it's gone.

Constraints

  • 0 <= key <= 10^6
  • At most 10^4 calls will be made to add, remove, and contains
Source: Custom Data Structures pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle