Mathwords logoMathwords

Hashing — Definition, Formula & Example

Hashing is a way to store and find data quickly. A hash function turns each key, such as an ID number, into a slot number in a fixed-size table, so you can jump straight to that slot without searching the whole list.

A hash function is a function h:U→{0,1,…,m−1}h: U \to \{0, 1, \dots, m-1\} from a universe of keys UU to the mm indices of a hash table. The set UU is usually far larger than mm. Two distinct keys k1≠k2k_1 \neq k_2 with h(k1)=h(k2)h(k_1) = h(k_2) form a collision. The division method uses h(k)=k mod mh(k) = k \bmod m. Under this method, two keys collide exactly when k1≡k2(modm)k_1 \equiv k_2 \pmod{m}.

Key Formula

h(k)=k mod mh(k) = k \bmod m
Where:
  • kk = The key being stored, a nonnegative integer
  • mm = The number of slots in the hash table, often chosen to be prime
  • h(k)h(k) = The slot index, the remainder when k is divided by m, between 0 and m − 1

Worked Example

Problem: A hash table has m=7m = 7 slots, numbered 0 through 6. It uses h(k)=k mod 7h(k) = k \bmod 7. Insert the keys 15, 23, 42, and 50 in that order. If a slot is already full, use linear probing: try the next slot, and wrap around after slot 6. Where does each key end up?
Step 1: Compute each key's remainder after dividing by 7. For example, 15=2⋅7+115 = 2 \cdot 7 + 1, so 15 hashes to slot 1.
h(15)=1,h(23)=2,h(42)=0,h(50)=1h(15) = 1,\quad h(23) = 2,\quad h(42) = 0,\quad h(50) = 1
Step 2: Keys 15 and 50 both hash to slot 1, so they collide. The reason is that their difference is a multiple of 7, which means the two keys are congruent mod 7.
50−15=35=5⋅7⟹50≡15(mod7)50 - 15 = 35 = 5 \cdot 7 \quad\Longrightarrow\quad 50 \equiv 15 \pmod{7}
Step 3: Key 15 already fills slot 1 and key 23 fills slot 2. Linear probing therefore sends 50 to slot 1 first, then slot 2, then slot 3, which is empty.
(1+2) mod 7=3(1 + 2) \bmod 7 = 3
Answer: Key 42 goes in slot 0, key 15 in slot 1, key 23 in slot 2, and key 50 in slot 3. Key 50 was moved because it collided with key 15.

Why It Matters

In a data structures course, hash tables are the standard way to get average constant-time lookup. Python dictionaries and Java HashMaps work this way. Cryptographic hash functions such as SHA-256 apply the same idea to file-integrity checks and digital signatures, where finding collisions must be computationally infeasible. Passwords need more than a fast hash: they are stored with salted, deliberately slow hashes such as bcrypt or Argon2.

Common Mistakes

Mistake: Thinking a good hash function never produces collisions.
Correction: When there are more possible keys than slots, the pigeonhole principle guarantees that some distinct keys share a slot. A good hash function spreads keys evenly so collisions are rare, and the table still needs a plan to handle them, such as probing or chaining.

Related Terms

  • Function — The kind of rule a hash function is
  • Discrete Mathematics — Course where hashing, modular arithmetic, and pigeonhole arguments are taught
  • Pigeonhole Principle — Explains why hash collisions must eventually happen
  • Remainder — Common hash functions use the division remainder