Skip to content

Commit 305e303

Browse files
committedJul 29, 2018
Add comments to HashTable hash function.
1 parent f1152bf commit 305e303

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed
 

‎src/data-structures/hash-table/HashTable.js

+10
Original file line numberDiff line numberDiff 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,

0 commit comments

Comments
 (0)
Please sign in to comment.