Hash function: any function that can be used to map data of arbitrary size to fixed-size values.
E.g: SHA256, SHA512, MD5 …
one-way algorithm
efficiently computable
randomly distributed
A data structure which maps keys to values. format: array of slots (buckets).
hash table uses hash functions to compute an index (hashcode) from key.
Measure how full is the hash table
e.g: if hash table has 100 slots, 69 slots are filled => load factor = 69/100 = 69%
two keys generate the same hash value
hash function = H(x) = x % 10
Insert(key, value): to the tail of linked list at index => O(1)
Search(key): when all the key is in the same slot => O(n). Average case O(1)
Delete(key): delete key in the linkedlist table[index] => O(n)
the hash table is just an array, one item per slot.
when array’s size >= number of keys (if the array is nearly full) => double its size
Search(key) => need to find value, keep probing until you either:
Delete(key)
Note: prefer using array when possible, because hashmap is slower.
Example:
- count the frequency of number <= 1000 => should use array- count the frequency of number <= 10^9 => have to use hashmap- count the frequency of string/ object => have to use hashmap
Quick Links
Legal Stuff
Social Media