diff --git a/DesignHashSet.py b/DesignHashSet.py new file mode 100644 index 00000000..ee219319 --- /dev/null +++ b/DesignHashSet.py @@ -0,0 +1,28 @@ +# This approach uses an array of fixed size and maps each key to an index using a hash function (key % size). +# Each index stores a bucket to handle collisions using chaining. +# All operations: add, remove, contains—run in O(1) average time because each bucket stays small. + +class MyHashSet: + + def __init__(self): + self.size = 1000 + self.buckets = [[] for _ in range(self.size)] + + def _hash(self, key): + return key % self.size + + def add(self, key: int) -> None: + idx = self._hash(key) + bucket = self.buckets[idx] + if key not in bucket: + bucket.append(key) + + def remove(self, key: int) -> None: + idx = self._hash(key) + bucket = self.buckets[idx] + if key in bucket: + bucket.remove(key) + + def contains(self, key: int) -> bool: + idx = self._hash(key) + return key in self.buckets[idx]