This post explains the LeetCode problem Design HashSet step by step: the problem statement, input and output format, constraints, a sample test case and a working solution in Java that you can study and run.

Design a HashSet without using any built-in hash table libraries.
To be specific, your design should include these functions:
- add(value): Insert a value into the HashSet.
- contains(value) : Return whether the value exists in the HashSet or not.
- remove(value): Remove a value in the HashSet. If the value does not exist in the HashSet, do nothing.
Example
Design HashSet Solution
MyHashSet hashSet = new MyHashSet();
hashSet.add(1);
hashSet.add(2);
hashSet.contains(1); // returns true
hashSet.contains(3); // returns false (not found)
hashSet.add(2);
hashSet.contains(2); // returns true
hashSet.remove(2);
hashSet.contains(2); // returns false (already removed)
Note
All values will be in the range of [0, 1000000].
The number of operations will be in the range of [1, 10000].
Please do not use the built-in HashSet library.
Design HashSet Solution in Java
class MyHashSet {
/** Initialize your data structure here. */
class Node {
int val;
Node next;
public Node(int val) {
this.val = val;
}
}
private int size;
private Node[] array;
/** Initialize your data structure here. */
public MyHashSet() {
this.size = 100;
this.array = new Node[100];
}
public void add(int key) {
int index = key % this.size;
if(this.array[index] == null) {
this.array[index] = new Node(key);
return;
} else {
Node p = this.array[index];
while(p != null && p.val != key)
p = p.next;
if(p != null)
return;
Node newHead = new Node(key);
newHead.next = this.array[index];
this.array[index] = newHead;
}
}
public void remove(int key) {
int index = key % this.size;
if(this.array[index] == null) {
return;
} else {
Node p = this.array[index];
if(p.val == key) {
this.array[index] = p.next;
return;
}
while(p.next != null && p.next.val != key)
p = p.next;
if(p.next == null)
return;
p.next = p.next.next;
}
}
/** Returns true if this set contains the specified element */
public boolean contains(int key) {
int index = key % this.size;
if(this.array[index] == null)
return false;
Node p = this.array[index];
while(p != null && p.val != key)
p = p.next;
return p != null;
}
}
/**
* Your MyHashSet object will be instantiated and called as such:
* MyHashSet obj = new MyHashSet();
* obj.add(key);
* obj.remove(key);
* boolean param_3 = obj.contains(key);
*/
If you have any doubts regarding this problem or need the solution in other programming languages then leave a comment down below .
Tip: try to solve the problem yourself before reading the solution. Use the code as a reference to check your approach, and make sure you understand why it works.
Comments
Post a Comment