hash tables - Alwynblake/seattle-javascript-401n12 GitHub Wiki
Hash Tables
https://www.hackerearth.com/practice/data-structures/hash-tables/basics-of-hash-tables/tutorial/
-
Assume that you have an object and you want to assign a key to it to make searching easy. To store the key/value pair, you can use a simple array like a data structure where keys (integers) can be used directly as an index to store values. However, in cases where the keys are large and cannot be used directly as an index, you should use 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:
- 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.
- The element is stored in the hash table where it can be quickly retrieved using hashed key.
- hash = hashfunc(key)
- index = hash % array_size