What's the point of a hash table?

后端 未结 11 1496
耶瑟儿~
耶瑟儿~ 2020-12-28 17:01

I don\'t have experience with hash tables outside of arrays/dictionaries in dynamic languages, so I recently found out that internally they\'re implemented by making a hash

11条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-28 17:38

    The idea of a hash table is to provide a direct access to its items. So that is why the it calculates the "hash code" of the key and uses it to store the item, insted of the key itself.

    The idea is to have only one hash code per key. Many times the hash function that generates the hash code is to divide a prime number and uses its remainer as the hash code.

    For example, suppose you have a table with 13 positions, and an integer as the key, so you can use the following hash function

    f(x) = x % 13

提交回复
热议问题