## Hash functions hash table = hash function + array ![](https://hackmd.io/_uploads/r11tnwSO3.jpg) there are some requirements for a hash function: - It needs to be consistent. For example, suppose you put in “apple” and get back “4”. Every time you put in “apple”, you should get “4” back. - It should map different words to different numbers. For example, a hash function is no good if it always returns “1” for any word you put in. In the best case, every different word should map to a different number. ```ruby= book = {} book['avocado'] = 1.49 book['avocado'] # => 1.49 ``` ## use case ### DNS resolution ![](https://hackmd.io/_uploads/r1CmyOrdn.jpg) ### Preventing duplicate entries ![](https://hackmd.io/_uploads/BybjJOBO3.jpg) ### Using hash tables as a cache ![](https://hackmd.io/_uploads/rJ3xbdru2.jpg) ![](https://hackmd.io/_uploads/ryiNbOrun.jpg) ### Recap To recap, hashes are good for • Modeling relationships from one thing to another thing • Filtering out duplicates • Caching/memorizing data instead of making your server do work ## Collisions ![](https://hackmd.io/_uploads/r1UrUdHOn.jpg) ![](https://hackmd.io/_uploads/H1n8LOrO2.jpg) ![](https://hackmd.io/_uploads/rygVw8dBd3.jpg) ### suppose you work at a grocery store where you only sell produce that starts with the letter A. ![](https://hackmd.io/_uploads/HkFY8dB_h.jpg) There are two lessons here: - Your hash function is really important. Your hash function mapped all the keys to a single slot. Ideally, your hash function would map keys evenly all over the hash. - If those linked lists get long, it slows down your hash table a lot. But they won’t get long if you use a good hash function! Hash functions are important. A good hash function will give you very few collisions. So how do you pick a good hash function? That’s coming up in the next section! ## Performance ![](https://hackmd.io/_uploads/HJfOvuHOh.jpg) ### Load factor ![](https://hackmd.io/_uploads/rJZyOOBOn.jpg) ![](https://hackmd.io/_uploads/BJIW_OHd2.jpg) ![](https://hackmd.io/_uploads/S1Uz_dS_n.jpg) ### resizing ![](https://hackmd.io/_uploads/HyOauOSuh.jpg) ![](https://hackmd.io/_uploads/rkf0ddH_h.jpg) A good rule of thumb is, resize when your load factor is greater than **0.7** ### A good hash function ![](https://hackmd.io/_uploads/SJGzKuH_2.jpg)