How to Design Hashset in Python
As we know that HashSet is a famous class in Java. HashSet is used to store the values using a hash table. In this tutorial, we will cover HashSet in Python. We will also learn about how we can design HashSet in Python.
HashSet data structure can be created without using any built-in hash table libraries. Before diving deep into this topic, let's understand its core concepts.
We can design HashSet without using any hash table libraries. Below are the multiple different functions -
Let's understand these methods by below example.
First, initialize the HashSet and call add(1) function. It will add 1 into the hash set. Call add(3), which will add 3, then call contains(1). It will check whether 1 is present or not in the hash set. Now we call contains(2), add(2), contains(2), remove(2), contains(2).
The output will be returned as true for 1 is present, false for 2 is not present, true for 2 is present, false for 2 is not present, respectively.
Basic Operations of HashSet in Python
We can perform some basic operations in HashSet using the following methods. Let's understand these methods.
Adding new values in HashSet
In the below example, we will add the value in the hash set using add() function. The add() function add the value one at a time. Let's see the following code.
Adding value: 2 Adding value: 7 Adding value: 6
Removing values in HashSet
We can remove the existing value using the remove() function. Let's understand the following code.
Adding value: 2 Adding value: 7 Adding value: 6 Removed value: 7 Removed value: 6
Checking if values exist in HashSet
In this example, we will demonstrate how we can check whether a particular value exists or does not use the contains() function. Let's understand the following code.
Adding value: 2 Adding value: 7 Adding value: 6 It contains: 2
Algorithm for the HashSet in Python
In the first step, we define one data structure called HashList. Then, we initialize an empty list as a new_list. Then, we define an update() function in which found will be storing a Boolean value False. Now, we use for loop for each index I and K. if the key is the same as 'k', then new_list[i]=k and found value set to True. The value will be inserted at the last of the list if no value is found.
The next step is to define the get() function, which we will use for the loop, and if the value of k is the same as the key, the output will be True; otherwise, False. If the key is the same as 'k', delete the value from the list new_list. The same process will be applied in the remove() function.
Now, we will create the Main class HashSet. This class will declare the initialization function where the key_space value = 2096. The hash_table will have a list of new_list type objects of size key_space. Then, we will create 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].
Let's see the step-wise implementation algorithm.
Implementation of HashSet in Python
Here we will implement the above algorithm and create Python program. We will define the two classes: HashSet and CreateHashset. Let's see the below code.
10 Add 10 6 Add 6 5 Add 5 Contains 10 : True Contains 3: False Contains 8 : False 2 Add 2 3 Add 3 Contains 2 : True Remove 2 Contains 2 : False Contains 3 : True [3, 5, 6, 10]
In the above code, we have created a verifyvalues class that will check the values in the given list. We have added value 10, 6 and 5 using add method. Then, we checked 10, 3 and 8 in the Hash set using contains().
If the values are present, it returns True, and if the value is not present, it returns False. We removed 2 using the remove() function, and values get removed from the hash set. And we used the same process for the other values.
In this tutorial, we have discussed how we can create the HashSet in Python, write the algorithm and implement it using the Python code. We have described the step-wise process to create HashSet. We have also explained basic operations that can be performed on the HashSet.
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week