File tree Expand file tree Collapse file tree 1 file changed +10
-0
lines changed
src/data-structures/hash-table Expand file tree Collapse file tree 1 file changed +10
-0
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,16 @@ export default class HashTable {
2525 * @return {number }
2626 */
2727 hash ( key ) {
28+ // For simplicity reasons we will just use character codes sum of all characters of the key
29+ // to calculate the hash.
30+ //
31+ // But you may also use more sophisticated approaches like polynomial string hash to reduce the
32+ // number of collisions:
33+ //
34+ // hash = charCodeAt(0) * PRIME^(n-1) + charCodeAt(1) * PRIME^(n-2) + ... + charCodeAt(n-1)
35+ //
36+ // where charCodeAt(i) is the i-th character code of the key, n is the length of the key and
37+ // PRIME is just any prime number like 31.
2838 const hash = Array . from ( key ) . reduce (
2939 ( hashAccumulator , keySymbol ) => ( hashAccumulator + keySymbol . charCodeAt ( 0 ) ) ,
3040 0 ,
You can’t perform that action at this time.
0 commit comments