We introduced the concept of ‘Hash table’ because we need a data structure that is suitable for search operations

One thing to remember is that ‘Hash tables’ are refined versions of ‘Arrays’

Why? Arrays are best suited for ‘accessibility’ and ‘search operations’ contains ‘accessibility operation’

So we need to build ‘hash tables’ upon ‘arrays’

See in Hash tables we have ‘keys’ which in turn converted into ‘indices’ whereas in arrays we don’t have this liberty.

We will build a hash table in the array of 10 continuous slots(0–9)

Key to index using a hash table

We have 10 names to store and the requirement is easy to search or lookup

For that, we will convert characters into alphabetical positions and add the numbers

Note we have 10 continuous slots(0–9) but we have resulting numbers of more than 9.

So I divide the result by the number of slots and this will give us the remainder.

The remainder can never be more than the number of slots

There is a name to this operation called ‘Modulo operation’

To reiterate, the hash function converts the key to some number

Then index is found by dividing it by array size

Hash Collision

When you notice the above table we have remainders for Finch, Jane and Ross, Lisbon.

We have only one slot but two names

When you encounter this type of scenario, it is called ‘Hash Collision’

Hash Collision = Same output(Index) for different input(Key)

There are many ways to avoid this collision

Before that let us identify the stakeholders here.

Hash function, Array size, Index or different data structure in case of collision

1)Hash Function — Try to put functions that avoid collision(Obvious)

2)ArraySize — Try to increase the array size and hence modulo operator range will increase

Avoiding collision using increased array size

3)Index — When collision comes, just start looking for the next available empty slot and store the element

4)Different data structures — Have an array of pointers. Pointers points to next location(Yes! Linked list)

An array of pointers to avoid collission

5)In fact, we usually implement a mix of all the above to get the best result.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Arjun Vaidy
Arjun Vaidy

Written by Arjun Vaidy

Founder of a startup. I explain things through first principles and intuitive mental models

No responses yet

Write a response