A hash function turns a key of any size into a number in a fixed range, called the key's hash value or hash code. A hash table uses that number as an array index, so it can store a key and later find it again in one step instead of searching.
H(x) = x % 10This one maps any whole number to a value from 0 to 9, the index of a slot in a table of 10.
For a table of 100 slots, use the last two digits: h(k) = k % 100, which is always 0 to 99.
The first two digits would be a poor choice. Numbers in the same area share them, so most keys would land in a few slots.
Give each letter a value (a = 1, b = 2, … z = 26), add them up, and take % 100. Different words can give the same sum:
A weighted sum fixes much of this by making each position count differently, for example h = (31·h + letter) for each letter. Then "ad" = 35 and "bc" = 65.
The first four are what a hash table needs. The last three matter for security uses, such as storing passwords, where someone may be trying to break the hash on purpose.
unordered_map and unordered_set, for fast insert and lookup.Type whole numbers from 0 to 999,999,999, separated by spaces or commas. Every function below maps them into the same hash space of m slots. Highlighted values are collisions: two or more keys that got the same slot in that column.