• Articles
  • Tutorials
  • Interview Questions

Hashing in Data Structure

This blog aims to provide a comprehensive understanding of hashing in data structures, its applications, and the various techniques involved.

Learn Data Structures and Algorithms in this comprehensive video course:

What is Hashing in Data Structure?

Hashing, a crucial principle in data structures, encompasses the conversion of data into an exclusive identifier referred to as a hash code or hash value. Its primary objective is to enable efficient storage and retrieval of data across diverse applications.

Let’s consider a simple example to understand how hashing works. Suppose we have a list of student records, and we want to store their information in a data structure for quick access. We can use hashing to achieve this. First, we define a hash function that takes the student’s name as input and produces a corresponding hash code. This hash code serves as the index or address where the student’s record will be stored in the data structure, which is typically a hash table.

For instance, let’s assume we have a hash function that calculates the sum of the ASCII values of the characters in a name and then takes the modulus of the sum with the size of the hash table. If we apply this hash function to the name “John,” the sum of the ASCII values of ‘J,’ ‘o,’ ‘h,’ and ‘n’ is 324. If the hash table size is 10, we can compute the hash code by taking 324 mod 10, which results in a hash code of 4.

Next, we insert the student’s record at the index, or position 4, in the hash table. The record could contain details like the student’s name, age, grade, and any other relevant information.

To retrieve a student’s record, we follow a similar process. We apply the hash function to the desired student’s name, obtain the hash code, and access the corresponding index in the hash table. In our example, if we want to retrieve John’s record, we calculate the hash code as before (which is 4) and then retrieve the data stored at index 4 in the hash table.

The utilization of hashing expedites data retrieval processes since the hash code generated by the hash function directly serves as an address, granting immediate access to the desired information within the hash table. Without hashing, one would be required to search through the entirety of the data structure, resulting in significantly slower access times, particularly for substantial datasets.

Interested in becoming a Full Stack Developer? Take our Full Stack Development Course to take your first step.

Why Do We Need Hashing in Data Structure?

Hashing plays a crucial role in data structures due to several key reasons. Here are some important reasons why hashing is essential:

  • Efficient Data Retrieval: Hashing enables fast access to data. Through a hash function, data is transformed into a unique hash code, which acts as an index to directly retrieve the corresponding location in a data structure like a hash table. This direct access ensures efficient data retrieval, even for large datasets with constant-time average-case complexity.
  • Search Optimization: Hashing reduces search time when retrieving specific elements. Instead of traversing the entire data structure, hashing allows direct access to the desired data location based on its hash code. This improves the overall efficiency and performance of search operations.
  • Unique Identification: Hashing ensures that each data piece has a distinct identifier, namely the hash code. The hash function maps data to specific hash codes, achieving uniqueness. This differentiation between similar or identical data items helps maintain data integrity within the structure.
  • Data Organization: Hashing offers an efficient way to organize data within a structure. Hash tables, the most common data structure used with hashing, allocate memory slots or buckets based on the hash codes. Each bucket holds data elements with corresponding hash codes, facilitating structured storage and retrieval.
  • Collision Resolution: Hashing addresses collisions, which occur when different data elements produce the same hash code. Techniques like separate chaining or open addressing are employed to handle these situations and ensure data integrity. These techniques allow multiple data items to be stored and retrieved correctly, even if their hash codes collide.
  • Performance Optimization: Hashing enables optimal performance for data operations like insertion, deletion, and retrieval. With a well-designed hash function and an appropriately sized hash table, these operations can be performed in constant time, resulting in efficient data management.

Want a comprehensive list of interview questions? Here are the Full Stack developer interview questions!

How Does Hashing in Data Structure Work?

Hashing in data structures operates by employing a hash function to transform a data item into a distinct hash code. This hash code serves as an index to store and retrieve the item within a data structure like a hash table. To illustrate this process, consider the following example:

Suppose we possess a set of student records that we intend to store in a hash table. Each student record comprises a distinct ID, name, and grade. The student ID will be utilized as the key for the hashing process.

  • Hash Function: First, we define a hash function that takes the student ID as input and produces a hash code. For simplicity, let’s assume our hash function simply takes the last two digits of the student ID as the hash code. So, if a student has an ID of 123456, the hash code will be 56.
  • Hash Table Initialization: We create an empty hash table with a fixed number of slots or buckets, usually determined based on the expected number of data items. In our example, let’s say we have a hash table with 100 slots.
  • Insertion: To insert a student record into the hash table, we apply the hash function to the student’s ID to determine the hash code. Using our hash function, if the student ID is 123456, the hash code will be 56. We then use this hash code to determine the slot or bucket in the hash table where the record will be stored. The student’s record is placed in the bucket corresponding to the hash code.
  • Retrieval: To retrieve a student record from the hash table, we again apply the hash function to the student ID to obtain the hash code. We use this hash code to locate the corresponding bucket in the hash table and retrieve the record stored there. Using the hash code 56, we access the bucket associated with that code and retrieve the student’s record.

It’s important to note that collisions can occur in hashing when different data items produce the same hash code. In such cases, collision resolution techniques, like separate chaining or open addressing, are employed to handle the collisions and ensure proper storage and retrieval of data.

Seeking knowledge about various types of polymorphism? Explore our detailed blog explaining the types of polymorphism in C++

Get 100% Hike!

Master Most in Demand Skills Now !

Learn what a Linear Data Structure is and how it works!

Types of Hashing in Data Structure

There are two types of hashing that are widely used in the data structure: Closed-Address Hashing and Open-Address Hashing. These are explained in detail below.

  • Closed-Address Hashing: Closed-Address Hashing, also known as Open Hashing or Separate Chaining, is a hashing technique where each slot (bucket) in the hash table stores a linked list of elements that have the same hash value. When a collision occurs, meaning multiple elements are mapped to the same hash value, they are stored in the same slot using separate chaining.

Here is how Closed-Address Hashing functions:

  • Initialization: A hash table is created with an array of slots, each initially empty.
  • Hashing: The hash function is applied to the key to determine the index (hash value) where the element should be stored.
  • Collision Handling: If there is already an element stored at that index, a linked list is used to handle hash collisions. The new element is appended to the linked list in the corresponding slot, allowing multiple elements to coexist at the same index.
  • Lookup: To retrieve an element, the hash function is applied to the key, and the linked list in the corresponding slot is traversed to find the desired element.

Here is an example of closed-address hashing represented in table form:

IndexKeyValue
0
127Data A
212Data B
33Data C
4
58Data D
620Data E
7
833Data F
9

In this illustrative example, we observe a hash table comprising 10 indices, ranging from 0 to 9. The hashing technique employed is known as closed-address hashing or separate chaining, which effectively manages collisions that may occur during the hashing process. Notably, each index within the table possesses the ability to store one or more key-value pairs.

In this particular scenario, the keys utilized are numeric values, while the associated values represent the data linked to each key. The responsibility of determining the appropriate index for storing each key-value pair lies with the hash function.

In the table above, we can see that keys 27, 12, and 3 are hashed to indices 1, 2, and 3, respectively. These indices contain the associated data: Data A, Data B, and Data C. Similarly, keys 8, 20, and 33 are hashed to indices 5, 6, and 8, respectively, and store the corresponding data: Data D, Data E, and Data F.

Indices 0, 4, 7, and 9 are currently empty. When a collision occurs, additional key-value pairs are stored in the same index using a collision resolution technique like linked lists or arrays.

This example demonstrates how closed-address hashing handles collisions by storing multiple key-value pairs at the same index, allowing efficient retrieval and storage of data.

Here is the basic syntax for closed-address hashing:

class Node:
    def __init__(self, key, value):
        self.key = key
        self.value = value
        self.next = None
class HashTable:
    def __init__(self, size):
        self.size = size
        self.table = [None] * size
    def hash_function(self, key):
        return key % self.size
    def insert(self, key, value):
        index = self.hash_function(key)
        if self.table[index] is None:
            self.table[index] = Node(key, value)
        else:
            current = self.table[index]
            while current.next is not None:
                current = current.next
            current.next = Node(key, value)
    def search(self, key):
        index = self.hash_function(key)
        current = self.table[index]
        while current is not None:
            if current.key == key:
                return current.value
            current = current.next
        return None
    def remove(self, key):
        index = self.hash_function(key)
        current = self.table[index]
        prev = None
        while current is not None:
            if current.key == key:
                if prev is None:
                    self.table[index] = current.next
                else:
                    prev.next = current.next
                return
            prev = current
            current = current.next
    def display(self):
        for i in range(self.size):
            current = self.table[i]
            elements = []
            while current is not None:
                elements.append((current.key, current.value))
                current = current.next
            if elements:
                print(f"Slot {i}: {elements}")
# Example usage
hash_table = HashTable(10)
hash_table.insert(5, "Apple")
hash_table.insert(15, "Banana")
hash_table.insert(25, "Orange")
hash_table.insert(35, "Mango")
hash_table.display()
# Output:
# Slot 5: [(5, 'Apple')]
# Slot 6: [(15, 'Banana')]
# Slot 7: [(25, 'Orange')]
# Slot 8: [(35, 'Mango')]
print(hash_table.search(25))
# Output: Orange
hash_table.remove(15)
hash_table.display()
# Output:
# Slot 5: [(5, 'Apple')]
# Slot 7: [(25, 'Orange')]
# Slot 8: [(35, 'Mango')]

The advantage of Closed-Address Hashing is that it can handle a large number of elements in each slot, as the linked list can grow dynamically. It also ensures constant-time complexity for insertions and lookups in the average case. However, it may suffer from performance degradation when the linked lists become long, resulting in increased search time.

  • Open-Address Hashing: Open-Address Hashing, also known as Closed Hashing or Linear Probing, is a hashing technique where all elements are stored directly within the hash table itself. When a collision occurs, the algorithm searches for an alternative slot in the hash table to place the element by using a probing sequence.

Let us see how Open-Address Hashing works:

  • Initialization: A hash table is created with an array of slots, each initially empty.
  • Hashing: The hash function is applied to the key to determine the initial index (hash value) where the element should be stored.
  • Collision Handling (Probing): If there is a collision at the initial index, a probing sequence is used to find an alternative slot in the hash table.  Quadratic probing (checking slots with quadratic increments), linear probing (checking the next slot),or double hashing (using a second hash function to determine the step size) are common probing methods.
  • Insertion: The algorithm finds an empty slot using the probing sequence and stores the element. If all slots are occupied, the hash table needs to be resized to accommodate more elements.
  • Lookup: The hash function is applied to the key to retrieve an element, and the probing sequence is used to search for the element in the hash table. If the desired element is not found, it means it was not inserted or has been removed.

Get a comprehensive understanding of Recursion in Data Structure with our in-depth blog post!

Here’s an example of an open-address hashing table in a tabular format:

IndexKeyValue
012John
13Mary
220Alice
335Bob
48Mark
5
617Jane
715Kate
8
929Sarah

We have a hash table with ten indices numbered from 0 to 9. Open-address hashing, also known as closed hashing or linear probing, is used to handle collisions. Each index in the table can store a key-value pair. The keys are numeric values, and the associated values represent the data linked to each key.

The table shows several key-value pairs stored in the hash table. For example, at index 0, the key 12 is mapped to the value “John”. Similarly, at index 1, the key 3 is mapped to the value “Mary”, and so on.

Please note that in open-address hashing, when a collision occurs, the algorithm probes through the table by linearly checking the next index until it finds an empty slot to store the key-value pair.

Here’s an example of Open-Address Hashing (Closed Hashing or Linear Probing) implemented in Python:

class HashTable:
    def __init__(self, size):
        self.size = size
        self.keys = [None] * size
        self.values = [None] * size
    def hash_function(self, key):
        return key % self.size
    def insert(self, key, value):
        index = self.hash_function(key)
        while self.keys[index] is not None:
            if self.keys[index] == key:
                # Key already exists, update the value
                self.values[index] = value
                return
            index = (index + 1) % self.size
        self.keys[index] = key
        self.values[index] = value
    def search(self, key):
        index = self.hash_function(key)
        while self.keys[index] is not None:
            if self.keys[index] == key:
                return self.values[index]
            index = (index + 1) % self.size
        return None
    def remove(self, key):
        index = self.hash_function(key)
        while self.keys[index] is not None:
            if self.keys[index] == key:
                self.keys[index] = None
                self.values[index] = None
                return
            index = (index + 1) % self.size
    def display(self):
        for i in range(self.size):
            if self.keys[i] is not None:
                print(f"Slot {i}: Key={self.keys[i]}, Value={self.values[i]}")
# Example usage
hash_table = HashTable(10)
hash_table.insert(5, "Apple")
hash_table.insert(15, "Banana")
hash_table.insert(25, "Orange")
hash_table.insert(35, "Mango")
hash_table.display()
# Output:
# Slot 5: Key=5, Value=Apple
# Slot 6: Key=15, Value=Banana
# Slot 7: Key=25, Value=Orange
# Slot 8: Key=35, Value=Mango
print(hash_table.search(25))
# Output: Orange
hash_table.remove(15)
hash_table.display()
# Output:
# Slot 5: Key=5, Value=Apple
# Slot 7: Key=25, Value=Orange
# Slot 8: Key=35, Value=Mango

The advantage of Open-Address Hashing is that it avoids the memory overhead of storing linked lists for collisions. It provides direct access to elements without requiring additional data structures. However, it can suffer from clustering, where consecutive elements are placed in nearby slots, leading to longer search times when the load factor is high.

Overall, the choice between Closed-Address Hashing and Open-Address Hashing depends on the specific requirements of the application, such as the expected number of elements, memory constraints, and performance considerations.

Hash Functions and Their Types

Hash functions are essential components of hashing techniques used in data structures and algorithms. They take an input (or key) and produce a fixed-size hash value or hash code. The hash value is used to efficiently index or locate data in hash tables or other data structures. Here are some common types of hash functions:

  • Division Hashing: This technique involves dividing the key value by the size of the hash table and using the remainder as the hash value. It is simple but can lead to clustering if the hash table is not properly sized.

Formula: hashValue = key % tableSize

Description: The hash value is obtained by taking the remainder of dividing the key by the size of the hash table.

  • Multiplication Hashing: In this technique, the key value is multiplied by a constant factor, and the fractional part of the result is used as the hash value. The constant factor should be carefully chosen to achieve a uniform distribution of hash values.

Formula: hashValue = floor(tableSize * ((key * A) % 1))

Description: The key is multiplied by a constant factor ‘A’, and the fractional part of the result is used as the hash value after scaling it with the table size.

  • Folding Hashing: Folding hashing involves dividing the key value into equal-sized parts and then performing some arithmetic operation (such as addition or XOR) on those parts to obtain the hash value.

Formula: Divide the key into equal-sized parts and perform an operation (e.g., addition or XOR) on those parts to obtain the hash value.

Description: The specific formula depends on the folding technique used. For example, if the key is divided into four parts, the hash value can be obtained by adding those parts together.

  • Mid-Square Hashing: This technique involves squaring the key value and extracting a portion of the resulting digits as the hash value. The extracted portion can be taken from the middle or any other fixed position.

Formula: hashValue = extractDigits((key^2), startPos, size)

Description: The key is squared, and a portion of the resulting digits is extracted as the hash value. The extraction can be done from the middle or any fixed position.

  • Universal Hashing: Universal hashing leverages a family of hash functions from which a hash function is chosen at runtime. This approach aids in diminishing collision probabilities by employing a randomly generated hash function.

Formula: hashValue = (a * key + b) % tableSize

Description: Universal hashing involves using a randomly generated hash function from a family of hash functions. ‘a’ and ‘b’ are random coefficients, and the key is multiplied by ‘a’, added to ‘b’, and then modulo table size.

  • Cryptographic Hashing: Cryptographic hash functions, such as MD5, SHA-1, and SHA-256, are designed to be computationally difficult to reverse or find collisions. They are primarily used for data integrity checks, password storage, and digital signatures.

Formula: Varies depending on the cryptographic hash function used (e.g., MD5, SHA-1, SHA-256).

Description: Cryptographic hash functions employ complex internal algorithms that are not as straightforward as the formulas mentioned earlier. Their design focuses on guaranteeing data integrity, security, and irreversibility.

  • Perfect Hashing: Perfect hashing aims to eliminate collisions altogether by using a two-level hashing scheme. It involves creating a hash table for the keys that collide in the first-level hash table.

Formula: 

First-level hash function: hashValue1 = key % tableSize1

Second-level hash function: hashValue2 = key % tableSize2

Description:

First-level hash function: The key is divided by the size of the first-level hash table, and the remainder is used as the first-level hash value. This determines the bucket in the first-level hash table where the key will be stored.

Second-level hash function: Within the selected bucket of the first-level hash table, the key is divided by the size of the second-level hash table, and the remainder is used as the second-level hash value. This determines the exact position within the selected bucket where the key will be stored.

Perfect hashing requires two levels of hash tables. The first-level hash table is used to store buckets, and each bucket contains a set of keys. The second-level hash table is used within each bucket to store keys without collisions. By using two levels of hashing, perfect hashing achieves constant-time access to the stored keys without any collisions.

To implement perfect hashing, the sizes of the first-and second-level hash tables need to be carefully chosen based on the number of keys and the expected collision rate. This process typically involves analyzing the distribution of keys to determine appropriate table sizes.

Benefits of Hashing

Here are some additional benefits of hashing that extend beyond its fundamental purpose:

Benefits of Hashing
  • Data Privacy: Hashing enhances data privacy by storing only hash values of sensitive information like passwords or personal identification numbers (PINs). This provides an additional layer of security, ensuring that the original data remains concealed even if the hash values are compromised.
  • Data Verification: Hashing enables data integrity checks by calculating hash values before and after data transmission or storage. By comparing these values, one can verify if the data has been tampered with. Matching hash values ensure that the data remains unchanged and uncorrupted.
  • Efficient Caching: Hashing plays a vital role in caching mechanisms, improving system performance. Hash-based lookup structures, such as hash maps or hash sets, eliminate redundant computations or database queries. The hashed keys serve as efficient references to cached data, reducing processing time and enhancing overall system performance.
  • Fast Indexing: Hashing enables rapid indexing and searching of data. Hash-based indexing structures, like hash tables or hash indexes, provide constant-time access to elements based on their hashed keys. This facilitates efficient searching and retrieval operations, particularly when handling large datasets.
  • Consistent Hashing: Consistent hashing is a technique used in distributed systems to evenly distribute data across multiple nodes or servers. It ensures minimal data redistribution when nodes are added or removed from the system. This enhances system scalability and fault tolerance, as the impact of node changes is minimized.

Also, Read on: HashMap in Java!

Limitations of Hashing

Hashing, like any other data structure or technique, has its limitations that are important to comprehend. Here are some of the constraints associated with hashing:

  • Collisions: One of the primary limitations of hashing arises when collisions occur. Collisions occur when two different keys produce the same hash value. Failure to handle collisions adequately can result in data loss or decreased performance. Techniques such as chaining or open addressing can be utilized to address collisions, but they may introduce additional complexity and overhead.
  • Key Uniqueness: Hashing assumes that each key is unique. However, in practical scenarios, it is possible for different keys to generate the same hash value, leading to incorrect data retrieval. This situation is commonly referred to as a collision. The quality of the hash function used significantly influences the likelihood of collisions occurring.
  • Loss of Order: Hashing does not maintain the original order of the elements. The hash function maps keys to random positions in the hash table, making it challenging to retrieve elements in a specific order. If preserving the order of the data is crucial, alternative data structures such as balanced binary trees or linked lists may be more suitable.
  • Complexity of Hash Functions: Designing effective hash functions can be complex. A well-designed hash function should generate evenly distributed hash values to minimize collisions and ensure efficient retrieval. Developing such a function requires careful consideration of the characteristics of the data being hashed. It may involve trade-offs between computation time, space utilization, and collision prevention.
  • Space Requirements: Hashing often necessitates additional space to handle collisions or maintain auxiliary data structures. For instance, in separate chaining, each hash table slot may need to store a linked list to accommodate multiple elements with the same hash value. This additional space usage can increase the memory footprint of the hash table and impact its efficiency.
  • Limited Range: Hash functions possess a restricted output range determined by the size of the hash table. If the number of keys significantly exceeds the table size, collisions become more probable, leading to reduced performance. It is essential to select an appropriate table size based on the expected number of keys and the desired performance level.

Comprehending these limitations is vital for making well-informed decisions when choosing a hashing technique or data structure for a particular application. It is essential to take into account the data’s characteristics, the anticipated workload, and the desired trade-offs between efficiency, memory usage, and ordering requirements.

Use Cases of Hashing

Hashing has a wide range of use cases across various domains. Here are six common scenarios where hashing is applied:

Use Cases of Hashing

Alt text -> Use Cases of Hashing

  • Data Integrity and Verification: Hashing is extensively used for data integrity checks and verification purposes. By calculating the hash value of a piece of data, such as a file or message, before and after transmission or storage, one can compare the hash values to ensure the data’s integrity. If the hash values match, it provides assurance that the data remains unaltered. This use case is particularly critical for secure communication protocols, digital signatures, and tamper-proof logging systems.
  • Password Storage and Authentication: Hashing serves as a fundamental approach to secure password storage. Instead of storing passwords directly, which can be susceptible to attacks, only the hash values of passwords are stored. During the login process, a user’s entered password is hashed and then compared to the stored hash value. This process guarantees that even if the hash values are compromised, the original passwords remain safeguarded. Commonly used hashing algorithms for password storage include bcrypt, scrypt, and Argon2.
  • Data Deduplication: Hashing plays a vital role in data deduplication, which involves identifying and eliminating duplicate data entries. By calculating hash values for data chunks or files, duplicate entries can be easily identified. This enables efficient storage utilization, reduces redundancy, and improves overall data management. Data deduplication is commonly used in backup systems, file synchronization, and content delivery networks.
  • Caching and Data Retrieval: Hashing is employed in caching mechanisms to improve performance. Hash-based lookup structures, such as hash maps or hash sets, are used to store frequently accessed data. The hash values serve as quick references to the cached data, allowing for rapid retrieval and reducing the need for repeated computations or database queries. This use case is prevalent in web caching, database query optimization, and in-memory data storage systems.
  • Data Routing in Distributed Systems: Hashing is crucial for efficient data routing in distributed systems. Consistent hashing, a specific hashing technique, is used to distribute data evenly across multiple nodes or servers. Each node is assigned a hash value range, and data is routed to the node whose hash value falls within that range. This enables load balancing and fault tolerance and minimizes data redistribution when nodes are added or removed from the system. Consistent hashing is commonly used in distributed hash tables, content delivery networks, and peer-to-peer networks.
  • Cryptographic Applications: Hashing is extensively employed in cryptographic applications to ensure data integrity, non-reversibility, and message authentication. Cryptographic hash functions generate fixed-length hash values that are unique to the input data. They are used in digital signatures, message authentication codes (MACs), secure key derivation, and blockchain technology. Popular cryptographic hash functions include SHA-256, MD5, and SHA-3.

Understanding these use cases makes it easier to deploy hashing in diverse settings effectively and securely, improving data management, security, and system performance.

Hashmaps in Data Structures

A hashmap, also known as a hash table or dictionary, is a type of data structure that implements an associative array abstract data type, a structure that can map keys to values. It uses a hash function to compute an index into an array of buckets or slots, from which the desired value can be found.

Here are some key characteristics of hashmaps:

Key-Value Pairs: Hashmaps store data in pairs, and each pair is known as an entry. The key acts as a unique identifier for a corresponding value, and it allows users to quickly access the value associated with the key.

Hash Function: The hash function is used to calculate the hash code, which is an integer derived from the key. The hash code is then used to find the index where the key-value pair is stored. The hash function is designed to ensure that different keys map to different indexes.

Collision Handling: Sometimes, the hash function may result in the same index for multiple keys. This is known as a collision. There are several methods to handle collisions, such as chaining (where each index in the hash table starts a linked list of entries) and open addressing (where if a collision occurs, the hashmap seeks an empty slot).

Time Complexity: Ideally, the access, search, insertion, and deletion operations of a hashmap are all constant time: O(1). However, in the worst-case scenario (where the hash function produces the same index for all keys), the time complexity can degrade to O(n), where n is the number of keys.

Dynamic Resizing: Some hashmaps automatically resize the underlying array when it’s too full to reduce the likelihood of collisions and maintain efficient operations.

Hashmaps are widely used because they offer fast data retrieval and are easy to implement. They are particularly useful when the amount of data is large and when it’s necessary to look up values quickly.

Here is a simple example of a hashmap in Python:

# Create a new dictionary
my_dict = {}
# Insert key-value pairs into the dictionary
my_dict["Apple"] = 1
y_dict["Banana"] = 2
my_dict["Cherry"] = 3
# Print the dictionary
print(my_dict)  # Output: {'Apple': 1, 'Banana': 2, 'Cherry': 3}
# Access a value by its key
print(my_dict["Apple"])  # Output: 1
# Update a value
my_dict["Apple"] = 10
print(my_dict["Apple"])  # Output: 10
# Remove a key-value pair
del my_dict["Apple"]
print(my_dict)  # Output: {'Banana': 2, 'Cherry': 3}
# Check if a key exists
if "Banana" in my_dict:
    print("Banana is in the dictionary")  # Output: Banana is in the dictionary
# Iterate over keys
for key in my_dict:
    print(key, my_dict[key])  # Output: Banana 2, Cherry 3

Conclusion

Hashing is a powerful tool in data structures that enables efficient data organization, retrieval, and manipulation. Its widespread use across various domains highlights its significance in optimizing performance. It ensures data integrity, enhancing security in data management systems.

Still, have queries? You can post your doubts on our Community page.

Course Schedule

Name Date Details
Python Course 11 May 2024(Sat-Sun) Weekend Batch
View Details
Python Course 18 May 2024(Sat-Sun) Weekend Batch
View Details
Python Course 25 May 2024(Sat-Sun) Weekend Batch
View Details