A collision occurs when two keys hash to the same slot. By the birthday paradox they appear surprisingly early, so every table needs a strategy. Separate chaining stores colliding keys in a list per slot. Open addressing stores them in other slots of the same array (linear probing, quadratic probing, double hashing).
Remember
/* chaining : slot -> list of keys
probing : try slot, slot+1, slot+2, ... */
Reference
Cheat Sheet
Keep these facts handy while you work with Collision Handling in C.
Topic
Detail
Chaining
List per slot
Linear probing
Next free slot
Double hashing
Second hash gives the step
Metric
Load factor n / m
Code
C Examples
Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Separate Chaining.
Example 1 — Count keys per slot
Six keys, five slots.
C
#include <stdio.h>
int main(void) {
int keys[] = {10, 15, 22, 27, 33, 40};
int per[5] = {0};
int collisions = 0;
for (int i = 0; i < 6; i++) per[keys[i] % 5]++;
for (int s = 0; s < 5; s++) {
printf("slot %d: %d key(s)\n", s, per[s]);
if (per[s] > 1) collisions += per[s] - 1;
}
printf("collisions: %d\n", collisions);
return 0;
}
Collisions are the keys beyond the first in each slot: 2 + 1 = 3.
Edge Cases & Pitfalls
Check these before relying on Collision Handling in real programs.
overwrite
Overwriting on collision
Storing straight into the slot silently loses the earlier key.
cluster
Primary clustering
Linear probing creates runs of occupied slots that slow later inserts.
Remember
Key Takeaways
Collisions are normal.
Chaining or probing.
Load factor drives cost.
One line: A collision occurs when two keys hash to the same slot.
Frequently Asked Questions
Two different keys producing the same table index.
Not in general, because there are more possible keys than slots. Only perfect hashing for a fixed key set can avoid them.
Continue with "C Separate Chaining" next. Resolve collisions with a linked list per slot and print the buckets. The C Hash Tables hub lists every topic in order.