# What Is Hashing in Data Structure With Example?

//

Angela Bailey

Hashing is a fundamental concept in data structures that allows for efficient storage and retrieval of data. In simple terms, hashing is the process of taking an input (or key) and converting it into a fixed-size value called a hash code. This hash code is then used to index and retrieve the corresponding data from a data structure called a hash table.

## Understanding Hashing

Hashing is widely used in computer science and plays a crucial role in various applications such as databases, caching, cryptography, and more. It provides a way to quickly locate data based on its unique identifier, thereby improving efficiency and reducing search times.

Let’s take an example to understand how hashing works:

Step 1: Consider a scenario where we have a list of names along with their respective phone numbers. We want to store this information in such a way that we can easily search for any given name and retrieve its associated phone number.

Step 2: To achieve this, we can use hashing. We can assign each name an integer value using a hashing function. This function will convert the name into its corresponding hash code.

Step 3: Let’s say we have the name “John” with the phone number “1234567890”. Using our hashing function, “John” gets converted into the hash code “35”.

Step 4: Now, we create a hash table where each index corresponds to a unique hash code. In this case, our hash table will have an index of size 100 (for simplicity).

## The Hash Table

To store our data efficiently, we will use arrays as our underlying data structure for the hash table. Each element of the array is called a bucket and can hold multiple key-value pairs if needed.

Let’s visualize our hash table:

```Index   |   Name   |   Phone Number
_______________________________________
0       |          |
1       |          |
2       |          |
. |          |
.

|          |
35      |   John    |  1234567890
. |          |
99      |          |
```

Step 5: We insert the name “John” and its corresponding phone number “1234567890” at index 35 in our hash table.

Step 6: Now, when we want to retrieve the phone number for the name “John”, we simply pass the name through our hashing function, which gives us the hash code “35”. We then go to index 35 in our hash table and retrieve the associated phone number.

## Benefits of Hashing

• Faster Retrieval: Hashing allows for constant-time retrieval of data, regardless of the size of the dataset. This makes it highly efficient for applications that involve searching and retrieving information.
• No Duplicate Keys: Hashing ensures that each key is unique within a given dataset. This prevents duplication of data and ensures data integrity.
• Optimal Memory Usage: By using a fixed-size hash table, memory usage can be optimized as it eliminates unnecessary space allocation.

### In Conclusion

Hashing is a powerful technique used in data structures to facilitate efficient storage and retrieval of data. It converts keys into unique hash codes, which are then used to index and retrieve corresponding values from a hash table. By using hashing, we can achieve faster retrieval times, eliminate duplicate keys, and optimize memory usage.

So the next time you need to store and retrieve data efficiently, consider using hashing!