File tree 1 file changed +10
-0
lines changed
src/data-structures/hash-table
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 {
25
25
* @return {number }
26
26
*/
27
27
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.
28
38
const hash = Array . from ( key ) . reduce (
29
39
( hashAccumulator , keySymbol ) => ( hashAccumulator + keySymbol . charCodeAt ( 0 ) ) ,
30
40
0 ,
You can’t perform that action at this time.
0 commit comments