What is a Hash Table in Data Structure? | Data Structure Tutorial
In this article, you will learn about the Hash Table in brief.
The hash table stores the item in the key-value pair where:
- Key: It is a unique value that is used for indexing the values.
- Value: It has data that are associated with the keys.
Hashing (Hash Function)
A new index is processed with the help of keys in the hash table and the data corresponding to that key is stored in the index. This process is known as hashing.
Hash Collision
If the hash function generates the same index for multiple keys, it becomes difficult for the compiler to understand which values need to be stored in that index. This is called a hash collision.
The hash collision can be resolved by using any one of the following techniques:
- Collision resolution by chaining
- Open Addressing through Linear/Quadratic Probing and Double Hashing
1. Collision resolution by chaining:
In this process, if a hash function is generating the same index for the various elements, these elements are stored at the same index by using the concept of a doubly-linked list.
2. Open Addressing:
In the chaining process, open addressing does not store multiple items in the index. In this process, each slot is either filled with a unique key or left NIL.
Different approaches used in open addressing are:
- Linear Probing:
In linear probing, the collision is resolved by checking on the next slot.
h(k, i) = (h’(k) + i) mod m
The issue with linear probing is that a cluster of corresponding slots is filled. When inserting a new item, the complete cluster is traversed. This adds to the time required to perform operations on the hash table.
- Quadratic Probing
The working of Quadratic Probing is very similar to linear probing but the spacing between the slots is increased by using the following relation.
h(k, i) = j1*i + j2*(i*i) mod m
Here,
- j1 and j2 are the positive auxiliary constants,
- i = { 0, 1, ………, }
- Double Hashing
When a collision happens after applying a hash function h(k), then another hash function is computed for finding the next slot.
h(k, i) = (h1*(k) + i* h2*(k)) mod m
Good Hash Functions
A good hash function does not fully prevent collisions completely, however it can reduce the number of collisions.
- Division Method
- Multiplication Method
- Universal Hashing
Applications of Hash Table
- Indexing data is a must
- It is implemented in cryptographic applications
- Constant time lookup and insertion are required.


