A hash table is a data structure that maps a set of keys to a set of values. A hash table is often called by many different names including:
myObj = {};
myObj['key'] = 'value';
myObj[1] = 'another value';
myObj['1'] // returns the string 'another value'
test = {}
test[myObj] = 'the last value';
test['[object Object]'] // returns the string 'the last value'
Fast lookup is very important in hashtables. What data-structure do you know that provides this ability?
how?
What's wrong with using an Array with numbers used to index into it?
Your array may end up being very sparse
hash_key = (key * LARGE_PRIME) % smaller_array_size
Chaining is a way to resolve collisions in a hash table. Instead of starting with an empty array, each array element contains a data structure to store collisions. A common data structure to use is a linked list, but others can be used such as a binary search tree or even another hash table. Whenever an element is inserted, both the key and the value are inserted into the data structure at that index.