The number of operations will be in the range of [1, 10000].
Please do not use the built-in HashSet library.
Solution:
class MyHashSet {
List<Integer>[] set;
/** Initialize your data structure here. */
public MyHashSet() {
set = new List[10000];
}
public void add(int key) {
int k = key % 10000;
if (set[k] == null) {
set[k] = new ArrayList();
}
set[k].add(key);
}
public void remove(int key) {
int k = key % 10000;
if (set[k] == null) return;
set[k].removeIf(Integer.valueOf(key)::equals);
}
/** Returns true if this set contains the specified element */
public boolean contains(int key) {
int k = key % 10000;
if (set[k] == null) return false;
return set[k].contains(key);
}
}
/**
* 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);
*/