# C#LeetCode刷题之#705-设计哈希集合​​​​​​​（Design HashSet）

contains(value) ：返回哈希集合中是否存在这个值。
remove(value)：将给定值从哈希集合中删除。如果哈希集合中没有这个值，什么也不做。

MyHashSet hashSet = new MyHashSet();

hashSet.contains(1);    // 返回 true

hashSet.contains(3);    // 返回 false (未找到)

hashSet.contains(2);    // 返回 true

hashSet.remove(2);

hashSet.contains(2);    // 返回  false (已经被删除)

Design a HashSet without using any built-in hash table libraries.

To be specific, your design should include these two 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.

MyHashSet hashSet = new MyHashSet();

hashSet.contains(1);    // returns true

hashSet.contains(2);    // returns true

hashSet.remove(2);

hashSet.contains(2);    // returns false (already removed)

Note:

All values will be in the range of [1, 1000000].
The number of operations will be in the range of [1, 10000].
Please do not use the built-in HashSet library.

```public class Program {

public static void Main(string[] args) {
var hashSet = new MyHashSet1();
var hashSet2 = new MyHashSet2();

hashSet.Contains(1);
hashSet.Contains(3);
hashSet.Contains(2);
hashSet.Remove(2);
hashSet.Contains(2);

Console.WriteLine();

hashSet2.Contains(1);
hashSet2.Contains(3);
hashSet2.Contains(2);
hashSet2.Remove(2);
hashSet2.Contains(2);

}

public class MyHashSet1 {

//此解法实在可耻！仅作演示
private List<int> _list = null;

public MyHashSet1() {
_list = new List<int>();
}

if(!_list.Contains(key)) {
}
}

public void Remove(int key) {
_list.Remove(key);
}

public bool Contains(int key) {
var res = _list.Contains(key);
Console.WriteLine(res);
return res;
}

}

public class MyHashSet2 {
private int[] buckets;

public MyHashSet2() {
buckets = new int[1000000];
}

private int GetHashCode(int key) {
//用本身值作为散列值
return key;
}

buckets[GetHashCode(key)] = 1;
}

public void Remove(int key) {
buckets[GetHashCode(key)] = 0;
}

public bool Contains(int key) {
var res = buckets[GetHashCode(key)] == 1;
Console.WriteLine(res);
return res;
}

}

}```

```True
False
True
False

True
False
True
False```