---
# System prepended metadata

title: CSPT27 Lecture 6

---

# CSPT27 Lecture 6
## Design Hash Set
```
from collections import deque
"""
myHashSet = MyHashSet()
myHashSet.add("key1")
myHashSet.add("key2")
x = myHashSet.contains("key1")
myHashSet.remove("key1")

"""
class MyHashSet:

    def __init__(self):
        self.arr = [None] * 10000
    
    def hashFunction(self, key):
        return hash(key) % len(self.arr)

    def add(self, key: int) -> None:
        hashIndex = self.hashFunction(key)
        if self.arr[hashIndex] is None:
            newList = deque()
            newList.append(key)
            self.arr[hashIndex] = newList
        elif key not in self.arr[hashIndex]:
            self.arr[hashIndex].append(key)

    def remove(self, key: int) -> None:
        hashIndex = self.hashFunction(key)
        if self.arr[hashIndex] is not None:
            try:
                self.arr[hashIndex].remove(key)
            except:
                pass

    def contains(self, key: int) -> bool:
        hashIndex = self.hashFunction(key)
        if self.arr[hashIndex] is not None:
            return key in self.arr[hashIndex]
        return False


# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)
```