
class MyHashSet {
private LinkedList[] buckets;
private static final int BASE = 769;
public MyHashSet() {
buckets = new LinkedList[BASE];
for (int i = 0; i < BASE; i++) {
buckets[i] = new LinkedList<Integer>();
}
}
private int hash(int key) {
return key % BASE;
}
public void add(int key) {
int h = hash(key);
Iterator<Integer> it = buckets[h].iterator();
while (it.hasNext()) {
int k = it.next();
if (k == key) {
return;
}
}
buckets[h].offer(key);
}
public void remove(int key) {
int h = hash(key);
Iterator<Integer> it = buckets[h].iterator();
while (it.hasNext()) {
Integer k = it.next();
if (k == key) {
buckets[h].remove(k);
return;
}
}
}
public boolean contains(int key) {
int h = hash(key);
Iterator<Integer> it = buckets[h].iterator();
while (it.hasNext()) {
int k = it.next();
if (k == key) {
return true;
}
}
return false;
}
}