Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions DesignHashSet.py
Original file line number Diff line number Diff line change
@@ -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]