Design HashSet in Python

Suppose we want to design a HashSet data structure without using any built-in hash table
libraries. There will be different functions like −
add(x) − Inserts a value x into the HashSet.
contains(x) − Checks whether the value x is present in the HashSet or not.
remove(x) − Removes x from the HashSet. In case the value does not exist in the HashSet, do nothing.
So, to test it Initialize the hash set, then call add(1), add(3), contains(1), contains(2), add(2), contains(2), remove(2), contains(2)., then the output will be true (1 is present), false (2 is not present), true (2 is present), false (2 is not present) respectively.
To solve this, we will follow these steps −
Define one data structure called Bucket, Initialize it like below
bucket := a new list
Define a function update(). This will take key
found := False
for each index i and key k in bucket, doif key is same as k, thenbucket[i]:= keyfound:= Truecome out from the loopif found false, theninsert key at the end of bucket
if key is same as k, thenbucket[i]:= keyfound:= Truecome out from the loop
bucket[i]:= key
found:= True
come out from the loop
if found false, theninsert key at the end of bucket
insert key at the end of bucket
Define a function get() . This will take keyfor each k in bucket, doif k is same as key, thenreturn Truereturn False
for each k in bucket, doif k is same as key, thenreturn Truereturn False
if k is same as key, thenreturn True
return True
return False
Define a function remove(). This will take keyfor each index i and key k in bucket, doif key is same as k, thendelete bucket[i]
for each index i and key k in bucket, doif key is same as k, thendelete bucket[i]
if key is same as k, thendelete bucket[i]
delete bucket[i]
Now create custom hashSet. There will be few methods as follows
Initialize this as follows −
key_space := 2096
hash_table:= a list of bucket type object of size key_space
Define a function add(). This will take keyhash_key:= key mod key_spacecall update(key) of hash_table[hash_key]
hash_key:= key mod key_space
call update(key) of hash_table[hash_key]
Define a function remove(). This will take keyhash_key:= keymodkey_spacedelete key from hash_table[hash_key]
hash_key:= keymodkey_space
delete key from hash_table[hash_key]
Define a function contains(). This will take keyhash_key:= keymodkey_spacereturn get(key) of hash_table[hash_key]
hash_key:= keymodkey_space
return get(key) of hash_table[hash_key]
Let us see the following implementation to get better understanding −
 Live Demo
class Bucket:
   def __init__(self):
      self.bucket=[]
   def update(self, key):
      found=False
      for i,k in enumerate(self.bucket):
         if key==k:
            self.bucket[i]=key
            found=True
            break
      if not found:
         self.bucket.append(key)
   def get(self, key):
      for k in self.bucket:
         if k==key:
            return True
      return False
   def remove(self, key):
      for i,k in enumerate(self.bucket):
         if key==k:
            del self.bucket[i]
class MyHashSet:
   def __init__(self):
      self.key_space = 2096
      self.hash_table=[Bucket() for i in range(self.key_space)]
   def add(self, key):
      hash_key=key%self.key_space
      self.hash_table[hash_key].update(key)
   def remove(self, key):
      hash_key=key%self.key_space
      self.hash_table[hash_key].remove(key)
   def contains(self, key):
      hash_key=key%self.key_space
      return self.hash_table[hash_key].get(key)
ob = MyHashSet()
ob.add(1)
ob.add(3)
print(ob.contains(1))
print(ob.contains(2))
ob.add(2)
print(ob.contains(2))
ob.remove(2)
print(ob.contains(2))
ob = MyHashSet()
ob.add(1)
ob.add(3)
print(ob.contains(1))
print(ob.contains(2))
ob.add(2)
print(ob.contains(2))
ob.remove(2)
print(ob.contains(2))
True
False
True
False