What is Hashing?
Hashing is a technique used in data structures that efficiently stores and retrieves data in
a way that allows for quick access.
Hashing involves mapping data to a specific index in a hash table (an array of items)
using a hash function. It enables fast retrieval of information based on its key.
The great thing about hashing is, we can achieve all three operations (search, insert
and delete) in O(1) time on average.
Hashing is mainly used to implement a set of distinct items (only keys) and
dictionaries (key value pairs).
Hashing uses mathematical formulas known as hash functions to do the transformation.
This technique determines an index or location for the storage of an item in a data
structure called Hash Table.
Components of Hashing
There are majorly three components of hashing:
1. Key: A Key can be anything string or integer which is fed as input in the hash function
the technique that determines an index or location for storage of an item in a data
structure.
2. Hash Function: Receives the input key and returns the index of an element in an
array called a hash table. The index is known as the hash index .
3. Hash Table: Hash table is typically an array of lists. It stores values corresponding to
the keys. Hash stores the data in an associative manner in an array where each data
value has its own unique index.
What is a Hash function?
A hash function creates a mapping from an input key to an index in hash table, this is
done through the use of mathematical formulas known as hash functions. For example:
Consider phone numbers as keys and a hash table of size 100.
Example:
Hash Table size: 10
Keys to insert: 1001, 1002, 1003
Hash Function: h(k) = k % 10
Apply Hash Function
For each key:
Key Hash Function h(k) = k % 10 Index in Hash Table
1001 1001 % 10 = 1 Store at index 1
1002 1002 % 10 = 2 Store at index 2
1003 1003 % 10 = 3 Store at index 3
Different Hash Function
Division Method
Take the remainder of the key divided by the size of the hash table.
h(k) = k % m
Key = 123, Table size m = 10
123 % 10 = 3 → Store at index 3
Mid-Square Method
Square the key, then extract the middle digits.
Steps:
Square the key
Extract middle r digits (based on table size)
Apply modulo if needed
Key = 123 → 123² = 15129
Extract middle 2–3 digits: 512
512 % 10 = 2
Folding Method
Break the key into equal parts, add them to get the index.
Steps:
Divide key into parts (2-3 digits each)
Add them together
Apply modulo with table size
Key = 123456 → split into 123 and 456
123 + 456 = 579
579 % 10 = 9
https://www.youtube.com/watch?v=6D5RaIGRPqA
https://www.youtube.com/watch?v=U1aEQvhcA9s
Collision handling in Hashing
A collision occurs in hashing when two different keys are assigned to the same index in
a hash table by the hash function.
Hash functions convert input (keys) into a fixed range of values (usually from 0 to
table_size - 1).
If there are more keys than slots, or if the hash function isn't well-designed, multiple
keys can map to the same slot.
Example
Suppose we have a hash table of size 10 and a hash function:
h(k) = k % 10
Now insert these keys:
101 → h(101) = 1
111 → h(111) = 1
Both keys map to index 1:
How to Handle Collisions?
1. Chaining: Store multiple elements at the same index using a list
2. Open Addressing: Find another empty spot in the table
o Linear Probing: .
o Quadratic Probing:
o Double Hashing:
What is chaining?
Chaining is a collision resolution technique in hashing, where each slot of the hash table
contains a linked list (or dynamic list) of entries. If multiple keys hash to the same index,
they are stored in a chain (list) at that index instead of replacing each other.
Let’s use a hash table of size 5 and the following hash function:
h(k) = k % 5
Keys to Insert:
10, 15, 20, 32, 22
Step-by-Step Hashing:
Key Hash Index
Function
10 10 % 5 = 0 0
15 15 % 5 = 0 0 (collision)
20 20 % 5 = 0 0 (collision)
32 32 % 5 = 2 2
22 22 % 5 = 2 2 (collision)
Hash Table with Chaining:
Index Chain (Linked List)
0 10 → 15 → 20
1 —
2 32 → 22
3 —
4 —
https://www.youtube.com/watch?v=iEsH4oNF6Vo
What is Linear Probing?
Linear Probing handles collisions by sequentially searching for the next available slot
in the hash table.
How It Works:
If a collision occurs at index i, check:
(i + 1) % table_size
(i + 2) % table_size
(i + 3) % table_size
...until an empty slot is found.
Example
Hash Table Size: 7
Hash Function: h(k) = k % 7
Keys to Insert: 10, 20, 15, 7, 32
No collisions yet
Now insert 27:
27 % 7 = 6 → index 6 is occupied by 20
Try (6 + 1) % 7 = 0 → occupied by 7
Try (6 + 2) % 7 = 1 → occupied by 15
Try (6 + 3) % 7 = 2 → empty → insert at index 2
Quadratic Probing:
Quadratic Probing is a collision resolution technique where we resolve collisions by
moving in quadratically increasing steps.
h(k, i) = (h(k) + i²) % table_size
h(k) = original hash index
i = collision attempt number (1, 2, 3, …)
Try i² steps away when a collision occurs.
Hash Table Size: 7
Hash Function: h(k) = k % 7
Keys to Insert: 10, 20, 15, 7, 32, 17
Step-by-Step Insertion
Key = 10
10 % 7 = 3 → Slot 3 is empty → insert at index 3
Key = 20
20 % 7 = 6 → Slot 6 is empty → insert at index 6
Key = 15
15 % 7 = 1 → Slot 1 is empty → insert at index 1
Key = 7
7 % 7 = 0 → Slot 0 is empty → insert at index 0
Key = 32
32 % 7 = 4 → Slot 4 is empty → insert at index 4
Key = 17
17 % 7 = 3 → Slot 3 is occupied (by 10)
Now use Quadratic Probing:
Double Hashing: