LeetCode 0705. Design HashSet Solution in Java, Python, C++, JavaScript, Go & Rust | Explanation + Code

CoderIndeed
0
0705. Design HashSet

Description

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

Implement MyHashSet class:

  • void add(key) Inserts the value key into the HashSet.
  • bool contains(key) Returns whether the value key exists in the HashSet or not.
  • void remove(key) Removes the value key in the HashSet. If key does not exist in the HashSet, do nothing.

 

Example 1:

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

Explanation
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 <= 106
  • At most 104 calls will be made to add, remove, and contains.

Solutions

Solution 1: Static Array Implementation

Directly create an array of size 1000001, initially with each element set to false, indicating that the element does not exist in the hash set.

When adding an element to the hash set, set the corresponding position in the array to true; when deleting an element, set the corresponding position in the array to false; when checking if an element exists, directly return the value at the corresponding position in the array.

The time complexity of the above operations is O(1).

PythonJavaC++GoTypeScript
class MyHashSet: def __init__(self): self.data = [False] * 1000001 def add(self, key: int) -> None: self.data[key] = True def remove(self, key: int) -> None: self.data[key] = False def contains(self, key: int) -> bool: return self.data[key] # Your MyHashSet object will be instantiated and called as such: # obj = MyHashSet() # obj.add(key) # obj.remove(key) # param_3 = obj.contains(key)(code-box)

Solution 2: Array of Linked Lists

We can also create an array of size SIZE=1000, where each position in the array is a linked list.

PythonJavaC++Go
class MyHashSet: def __init__(self): self.size = 1000 self.data = [[] for _ in range(self.size)] def add(self, key: int) -> None: if self.contains(key): return idx = self.hash(key) self.data[idx].append(key) def remove(self, key: int) -> None: if not self.contains(key): return idx = self.hash(key) self.data[idx].remove(key) def contains(self, key: int) -> bool: idx = self.hash(key) return any(v == key for v in self.data[idx]) def hash(self, key) -> int: return key % self.size # Your MyHashSet object will be instantiated and called as such: # obj = MyHashSet() # obj.add(key) # obj.remove(key) # param_3 = obj.contains(key)(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !