The problem of hash function of JavaScript

    var loseloseHashCode = function (key) {
        var hash = 0;
        for (var i = 0; i < key.length; iPP) {
            hash += key.charCodeAt(i);
        }
        return hash % 37;
    };
    
    this.put = function(key, value) {
        var position = loseloseHashCode(key); //{5}
        console.log(position + " - " + key); //{6}
        table[position] = value; //{7}
    };

question: seeing the last return hash% 37 of the hash function in a book, I tested that it would be overwritten. For example, when key="Abc" and key="fbc" , because the sum of the ASCII code values of Abc is 262 and the sum of ASCII values of fbc is 299. The two of them have their own Mod37=3. So a value is inserted in the position=3 position, and the result is an override. The book is for relatively small values, but this will produce coverage, ah, is this the original meaning of the hash function?

Mar.01,2021

maybe it's just for the sake of, for example, conflict is inevitable. There are corresponding methods to resolve conflicts .

< hr >

conflict

  • arrays cannot be infinitely long, and the same indexes generated will cause conflicts.

solution

  • Zipper method, index stores linked lists, keys and values and next are stored in linked lists. At the same time, there is another problem, the importance of the hash function, all the keys should be mapped evenly.
  • Zipper variants, do not store linked lists, save trees or hash tables.
  • Open addressing, conflict will find the next non-conflict location, and the capacity will be expanded when the capacity is full.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b365e5-2bfee.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b365e5-2bfee.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?