var MyHashSet = function() {
this.BASE = 769;
this.data = new Array(this.BASE).fill(0).map(() => new Array());
};
MyHashSet.prototype.add = function(key) {
const h = this.hash(key);
for (const element of this.data[h]) {
if (element === key) {
return;
}
}
this.data[h].push(key);
};
MyHashSet.prototype.remove = function(key) {
const h = this.hash(key);
const it = this.data[h];
for (let i = 0; i < it.length; ++i) {
if (it[i] === key) {
it.splice(i, 1);
return;
}
}
};
MyHashSet.prototype.contains = function(key) {
const h = this.hash(key);
for (const element of this.data[h]) {
if (element === key) {
return true;
}
}
return false;
};
MyHashSet.prototype.hash = function(key) {
return key % this.BASE;
}
/**
* Your MyHashSet object will be instantiated and called as such:
* var obj = new MyHashSet()
* obj.add(key)
* obj.remove(key)
* var param_3 = obj.contains(key)
*/
class MyHashSet {
private static final int BASE = 769;
private LinkedList[] data;
/** Initialize your data structure here. */
public MyHashSet() {
data = new LinkedList[BASE];
for (int i = 0; i < BASE; ++i) {
data[i] = new LinkedList<Integer>();
}
}
public void add(int key) {
int h = hash(key);
Iterator<Integer> iterator = data[h].iterator();
while (iterator.hasNext()) {
Integer element = iterator.next();
if (element == key) {
return;
}
}
data[h].offerLast(key);
}
public void remove(int key) {
int h = hash(key);
Iterator<Integer> iterator = data[h].iterator();
while (iterator.hasNext()) {
Integer element = iterator.next();
if (element == key) {
data[h].remove(element);
return;
}
}
}
/** Returns true if this set contains the specified element */
public boolean contains(int key) {
int h = hash(key);
Iterator<Integer> iterator = data[h].iterator();
while (iterator.hasNext()) {
Integer element = iterator.next();
if (element == key) {
return true;
}
}
return false;
}
private static int hash(int key) {
return key % BASE;
}
}
/**
* 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);
*/
class MyHashSet {
private:
vector<list<int>> data;
static const int base = 769;
static int hash(int key) {
return key % base;
}
public:
/** Initialize your data structure here. */
MyHashSet(): data(base) {}
void add(int key) {
int h = hash(key);
for (auto it = data[h].begin(); it != data[h].end(); it++) {
if ((*it) == key) {
return;
}
}
data[h].push_back(key);
}
void remove(int key) {
int h = hash(key);
for (auto it = data[h].begin(); it != data[h].end(); it++) {
if ((*it) == key) {
data[h].erase(it);
return;
}
}
}
/** Returns true if this set contains the specified element */
bool contains(int key) {
int h = hash(key);
for (auto it = data[h].begin(); it != data[h].end(); it++) {
if ((*it) == key) {
return true;
}
}
return false;
}
};
/**
* 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);
*/
const base = 769
type MyHashSet struct {
data []list.List
}
/** Initialize your data structure here. */
func Constructor() MyHashSet {
return MyHashSet{make([]list.List, base)}
}
func (this *MyHashSet) hash(key int) int {
return key % base
}
func (this *MyHashSet) Add(key int) {
if !this.Contains(key) {
h := this.hash(key)
this.data[h].PushBack(key)
}
return
}
func (this *MyHashSet) Remove(key int) {
h := this.hash(key)
for e := this.data[h].Front(); e != nil; e = e.Next() {
if e.Value.(int) == key {
this.data[h].Remove(e)
return
}
}
}
/** Returns true if this set contains the specified element */
func (this *MyHashSet) Contains(key int) bool {
h := this.hash(key)
for e := this.data[h].Front(); e != nil; e = e.Next() {
if e.Value.(int) == key {
return true
}
}
return false
}
/**
* Your MyHashSet object will be instantiated and called as such:
* obj := Constructor();
* obj.Add(key);
* obj.Remove(key);
* param_3 := obj.Contains(key);
*/