72. Design Hashset
08 Sep 2020 | Daily Algorithms
-
Since we do not need same elements multiple times, I used boolean array that can easily return the presence of the element in the array.
-
As per the constraints the size of the array is 1000000 and all elements are initialised to false.
-
Lets take following test case :-
class MyHashSet {
public:
/** Initialize your data structure here. */
bool arr[1000000]
{
false
};
MyHashSet() {
}
void add(int key) {
arr[key] = true;
}
void remove(int key) {
arr[key] = false;
}
/** Returns true if this set contains the specified element */
bool contains(int key) {
return arr[key];
}
};
/**
* Your MyHashSet object will be instantiated and called as such:
* MyHashSet* obj = new MyHashSet();
* obj->add(key);
* obj->remove(key);
* bool param_3 = obj->contains(key);
*/
-
Since we do not need same elements multiple times, I used boolean array that can easily return the presence of the element in the array.
-
As per the constraints the size of the array is 1000000 and all elements are initialised to false.
-
Lets take following test case :-
class MyHashSet {
public:
/** Initialize your data structure here. */
bool arr[1000000]
{
false
};
MyHashSet() {
}
void add(int key) {
arr[key] = true;
}
void remove(int key) {
arr[key] = false;
}
/** Returns true if this set contains the specified element */
bool contains(int key) {
return arr[key];
}
};
/**
* Your MyHashSet object will be instantiated and called as such:
* MyHashSet* obj = new MyHashSet();
* obj->add(key);
* obj->remove(key);
* bool param_3 = obj->contains(key);
*/
Comments