ligaroba / datastructures-and-algorithms

For beginners
1 stars 0 forks source link

I think collisions can be handled better #2

Open jmnyarega opened 5 years ago

jmnyarega commented 5 years ago

https://github.com/ligaroba/datastructures-and-algorithms/blob/7086cc6a286d80d9e7ffa5e1f56b75a6eb851f6b/Hashtables/hash_map_ds.py#L6-L10

Check out this resource handling hash table collisions

Since a hash function gets us a small number for a key which is a big integer or string, there is a possibility that two keys result in the same value. The situation where a newly inserted key maps to an already occupied slot in the hash table is called collision and must be handled using some collision handling technique.

jmnyarega commented 5 years ago
function hash(string, arr) {
   const H = 37; // hash value
   let total = 0;
   for (let i = 0; i < string.length; i++) {
      total += H * total + string.charCodeAt(i);
   }
   total = total % arr.length;
   return parseInt(total);
}