HashSet | How to Design HashSet in Python

Introduction

In python, we have discussed many concepts and conversions. In this tutorial, we will be talking about HashSet in python. We all know HashSet is a popular class in  Java. It’s used to store values using a hash table. But. In python, We will learn about how we can design HashSet in python.

What is HashSet in python?

Let us suppose we want to design a HashSet data structure in python without using any built-in hash table libraries. There will be multiple different functions like −:

  • add(x) − It is used to Insert a value x into the HashSet.
  • contains(x) − It is used to check whether the value x is present in the HashSet or not.
  • remove(x) − It is used to remove x from the HashSet. In case the value does not exist in the HashSet, it will do nothing.

Let us take an example to get more familiar with the functions:

Firstly, we will initialize the Hash Set, then call the add(1) function, which will add 1 in the hash set, then add(3), which will add 3, then contains(1), which will see that the element is present in the hash set or not, then contains(2), add(2), contains(2), remove(2), contains(2).

The Output should come as true for 1 is present, false for 2 is not present, true for 2 is present, false for 2 is not present, respectively.

Algorithm for the HashSet in python

Firstly, we will be defining one data structure which will be called checkingvalues. Then, we will be initializing it by making a list named mathfun. Then, defining a function update in which found will be storing a boolean value False. And then, we will apply for loop for each index of I and k. If the key is the same as ‘k,’ then, mathfunc[i]=k and found value set to True. If the value is not found, the value will be inserted at the last of the list.

After that, we will define the get function, we will apply for loop, and if the value of k is the same as key then, the output gets returned True. Otherwise False. After that remove function, we will apply for loop for each index of I and k. If the key is the same as ‘k,’ delete the value from the list mathfun.

Now, we will create the Main class HashSet. Inside the class, we will declare the Initialization function, where key_space value =2096. hash_table will be having a list of mathfun type objects of size key_space. Then, we will define add function, in which hash_key = key % key_space, and update the key of hash_table[hash_key]. After that, we will call the remove function, in which hash_key = key % key_space, and delete the key of hash_table[hash_key]. After that, we will call the contains function, in which
hash_key = key % key_space, and get the key of hash_table[hash_key].

Example of HashSet in python

In this example, we will be Defining two classes: checkingvalues and HashSet. Inside both, the classes we have defines many functions for operating in HashSet in Python. At last, we have created the object of the HashSet class and then given the input. Let us look at the example for understanding the concept in detail.

Explanation of code:

Firstly, the execution of the program starts from the class HashSet. Inside the class, we have called the initialization function. After that, 3 more functions are called add() function for adding the values in the hashtable from the input, remove() function for removing the values from the hashtable if passed in input, and contains() function for checking the value is present in the hashtable or not. Next, there is a display() function in which we will see all the present values in the hash set.

After that, we have defined another class with name checkingvalues. Inside the class, we have called the initialization function, which will be having a list with the name mathfun=[] which stores all the elements added and gets removed if the element is removed. after that, we will be having two more function update() which will update the value in the list. And get() function which will help us to get the output of the required query.

#Design HashSet in python
#checking the values and will return the output class
class checkingvalues:
   #initialization function which has list mathfun
   def __init__(self):
      self.mathfun=[]
    
   #update vales function
   def update(self, key):
      found=False
      for i,k in enumerate(self.mathfun):
         if key==k:
            self.mathfun[i]=key
            found=True
            break
      if not found:
         self.mathfun.append(key)
   
   #get values function
   def get(self, key):
      for k in self.mathfun:
         if k==key:
            return True
      return False
   #remove values function
   def remove(self, key):
      for i,k in enumerate(self.mathfun):
         if key==k:
            del self.mathfun[i]

#class HashSet main class
class HashSet:
   #Initialization function
   def __init__(self):
      self.key_space = 2096
      self.hash_table=[checkingvalues() for i in range(self.key_space)]
   def hash_values(self, key):
       hash_key=key%self.key_space
       return hash_key
   #add function
   def add(self, key):
      self.hash_table[self.hash_values(key)].update(key)
   #remove function
   def remove(self, key):
      self.hash_table[self.hash_values(key)].remove(key)
   #contains function
   def contains(self, key):

      return self.hash_table[self.hash_values(key)].get(key)
   def display(self):
       ls=[]
       for i in self.hash_table:
           if len(i.mathfun)!=0:ls.append(i.mathfun[0])
       print(ls)
 
   
       
ob = HashSet()
print(ob.hash_values(5))
print("Add 5 ")
ob.add(5)
print(ob.hash_values(6))
print("Add 6 ")
ob.add(6)
print(ob.hash_values(7))
print("Add 7 ")
ob.add(7)
print("Contains 5 : ",ob.contains(5))
print("Contains 7: ",ob.contains(7))
print("Contains 10 : ",ob.contains(10))
print(ob.hash_values(2))
print("Add 2 ")
ob.add(2)
print(ob.hash_values(3))
print("Add 3 ")
ob.add(3)
print("Contains 2 : ",ob.contains(2))
print("Remove 2 ")
ob.remove(2)
print("Contains 2 : ",ob.contains(2))
print("Contains 3 : ",ob.contains(3))
ob.display()

Output:

Hashset Example

Explanation of input:

  • Firstly, we have added value 5.
  • Then, we have added the values 6 and 7 too.
  • After that, we have checked that if 5, 7, and 10 are present in the Hash set with contains() function.
  • If the values are present, it returns True, and if the value is not present, it returns False.
  • Then, again we have added two values, i.e., 2 and 3.
  • Then, we have checked if 2 is present or not.
  • After that, we have removed a value 2 with the help of the remove() function, and the value gets removed from the hash set.
  • At last, we have checked that values 2 and 3 are present in the hash set or not with the help of contains a method().
  • The value 2 is removed just previously, so it returns False, and the value 3 is present, so the function returns it True.

Examples of HashSet in Python

In all the following examples, we’ve saved the HashSet Class (from the above section) in a file named hs.py. We’ll use this file to import the HashSet Class in our Python codes. This way, we can prevent the unnecessary declaration of HashSet class every time in our code.

1. Adding new values in HashSet Python

In this example, we will get to know how we can add the values in the hash set with the help of the following code.

from hs import HashSet
   
ob = HashSet()
ob.add(5)
ob.add(6)
ob.add(7)

Output:

 Adding new values in HashSet

2. Removing values in HashSet Python

In this example, we will get to know how we can remove the values in the hash set with the help of the following code.

from hs import HashSet
   
ob = HashSet()
ob.add(5)
ob.add(6)
ob.add(7)
ob.remove(6)
ob.remove(7)

Output:

Removing values in HashSet

3. Checking if values exist in HashSet Python

In this example, we will get to know how we can see if the value is present in the hash set or not with the help of the following code.

from hs import HashSet
   
ob = HashSet()
ob.add(5)
ob.add(6)
ob.add(7)
ob.contains(7)

Output:

Checking if values exist in HashSet Python

Conclusion

In this tutorial, we have learned about the concept of Design HashSet in Python. We have seen what HashSet is in python? Algorithm of writing the code on HashSet. Then we have explained HashSet in python with the proper code snippet. The example is explained in detail.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments