How to Design a 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.

A HashSet is a fundamental data structure in programming, commonly found in languages such as Java. It belongs to the Java Collections Framework and serves as an implementation of the set interface. The distinctive feature of a HashSet is its ability to store elements in a way that facilitates efficient checking for the existence of specific elements and ensures uniqueness within the set. Unlike structures such as lists, a HashSet does not maintain any specific order among its elements.

One key characteristic of a HashSet is its guarantee of uniqueness; it does not allow duplicate elements. Operations like adding, removing, and checking for the presence of elements typically have constant-time average performance, making it an efficient choice for such tasks. However, it's essential to note that the order of elements in a HashSet is not guaranteed.

Key Characteristics:

Uniqueness: A HashSet does not allow duplicate elements. It uses the equals() method to check for duplicates, ensuring that every element in the set is unique.

No Order: The elements in a HashSet are not stored in any particular order. If you need to maintain the order of elements, you might consider using a LinkedHashSet, which maintains the order of insertion.

Underlying Data Structure: Internally, a HashSet uses a hash table to store elements. This allows for constant-time average complexity for basic operations like add, remove, and contains.

Null Elements: A HashSet allows one null element. If you attempt to add a duplicate null element, it will replace the existing one.

Introduction

We can design HashSet without using any hash table libraries. Below are the multiple different functions -

add(x) - The add(x) method is mainly used to insert a value x into the HashSet.

contains(x) - The contains(x) method is mainly used to check whether a value x is present in the HashSet or not.

remove(x) - The remove(x) method is mainly used to delete x from the HashSet. If the HashSet doesn't have any value, it will do nothing.

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.

Example -

Output:

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.

Example -

Output:

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.

Example -

Output:

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.

Algorithm -

  • Create data structure called HashSet, Initialize it like below
  • new_list = []
  • Define a function update(). This will take key
  • found := False
  • for each index i and key k in new_list, do
    • if key is same as k, then
      • new_list[i]:= key
      • found:= True
      • come out from the loop
    • if found false, then
      • insert key at the end of new_list
  • Define a function get() . This will take key
    • for each k in new_list, do
      • if k is same as key, then
        • return True
      • return False
  • Define a function remove(). This will take key
    • for each index i and key k in new_list, do
      • if key is same as k, then
        • delete new_list[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 key
    • hash_key:= key mod key_space
    • call update(key) of hash_table[hash_key]
  • Define a function remove(). This will take key
    • hash_key:= keymodkey_space
    • delete key from hash_table[hash_key]
  • Define a function contains(). This will take key
    • hash_key:= keymodkey_space
    • return get(key) of hash_table[hash_key]

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.

Code -

Output:

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]

Explanation:

  • verifyvalues class: This class deals with a rundown of values (new_list) and gives techniques to refresh, check for presence, and eliminate values.
  • __init__ technique: Introduces a vacant rundown for each occasion.
  • update technique: Updates a current worth or affixss another worth to the rundown.
  • get technique: Checks in the event that a worth exists in the rundown.
  • eliminate strategy: Eliminates a predefined esteem from the rundown.
  • HashSet class: This is the primary execution of the HashSet.
  • __init__ technique: Introduces the HashSet with a predefined key space and makes a cluster (hash_table) of verifyvalues examples for taking care of impacts.
  • hash_values technique: Works out the hash key for a given info key utilizing the modulo activity.
  • add strategy: Adds a key to the HashSet by refreshing the comparing verifyvalues object in the hash_table.
  • eliminate technique: Eliminates a key from the HashSet.
  • contains strategy: Checks if a vital exists in the HashSet.
  • show technique: Prints the main component of each non-void verifyvalues list, offering a depiction of the information circulation.
  • Usage example: The code shows the use of the HashSet by adding keys (10, 6, 5), checking for presence, and showing some data about the inner state.





Latest Courses