Class 35 Hash Tables - Alwynblake/seattle-javascript-401n12 GitHub Wiki

https://www.hackerearth.com/practice/data-structures/hash-tables/basics-of-hash-tables/tutorial/

  • Hashing is a technique that is used to uniquely identify a specific object from a group of similar objects. Some examples of how hashing is used in our lives include:

  • In universities, each student is assigned a unique roll number that can be used to retrieve information about them.

  • In libraries, each book is assigned a unique number that can be used to determine information about the book, such as its exact position in the library or the users it has been issued to etc.

In both these examples the students and books were hashed to a unique number.

In hashing, large keys are converted into small keys by using hash functions. The values are then stored in a data structure called hash table. The idea of hashing is to distribute entries (key/value pairs) uniformly across an array. Each element is assigned a key (converted key). By using that key you can access the element in O(1) time. Using the key, the algorithm (hash function) computes an index that suggests where an entry can be found or inserted.

Hashing is implemented in two steps:

  1. An element is converted into an integer by using a hash function. This element can be used as an index to store the original element, which falls into the hash table.
  2. The element is stored in the hash table where it can be quickly retrieved using hashed key.
  • hash = hashfunc(key)
  • index = hash % array_size

To achieve a good hashing mechanism, It is important to have a good hash function with the following basic requirements:

  • Easy to compute: It should be easy to compute and must not become an algorithm in itself.

  • Uniform distribution: It should provide a uniform distribution across the hash table and should not result in clustering.

  • Less collisions: Collisions occur when pairs of elements are mapped to the same hash value. These should be avoided.

  • Note: Irrespective of how good a hash function is, collisions are bound to occur. Therefore, to maintain the performance of a hash table, it is important to manage collisions through various collision resolution techniques.