References

https://towardsdatascience.com/8-common-data-structures-every-programmer-must-know-171acf6a1a42

https://yourbasic.org/algorithms/hash-tables-explained/

https://www.tutorialspoint.com/data_structures_algorithms/hash_data_structure.htm

Concept

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/ebf5253a-ead4-4ad8-82e6-13ce9848b1c6/example.jpg

Hash tables are very similar to direct address tables. Actually, it's an optimization of it.

A hash table is an unordered collection of key-value pairs (which means that it stores data in an associative manner), where each key is unique, implemented through an array (exactly like a direct address table). And that's why hash tables offers a combination of efficient lookup, insert and delete operations. The main difference, here, between the direct address table and the hash table is that the hash table uses a hash technique to generate an index (that it's going to be our data position into our array). We call it the hash function. The hash function is responsible for overcoming one of the biggest problems in direct address tables: scenarios with a large number of key-value pairs.

Hash Function

The hash function (h) is a special function that converts a range of key values into a range of indexes of an array. To do so, it uses the modulo operator.

<aside> đŸ’¡ The modulo operator returns the remainder of a division, after one number is divided by another (called the modulus of the operation). For example, the expression "5 % 2" would evaluate to 1, because 5 divided by 2 has a quotient of 2 and a remainder of 1, while "9 % 3" would evaluate to 0, because the division of 9 by 3 has a quotient of 3 and a remainder of 0.

</aside>

So, the formula for the hash function is: h(k) = k % m

Where:

Example: