Understand Linear Probing in Hash Maps. Watch how the algorithm finds the next available slot when collisions occur.
About this algorithm
Linear probing is a way to build a hash map in a single array where each slot holds at most one entry. When a key's home slot is already taken, it tries the next slot, then the next, wrapping round at the end, until it finds a free one. Lookups follow the same path, so a key is always somewhere between its home slot and the first empty slot after it.
Choose linear probing when memory layout matters: all entries sit side by side in one array, there are no list nodes or pointers, and walking to the next slot is very cheap for the CPU cache. The cost is that it only works well while the table is fairly empty (a load factor under about 0.7 is a common rule), and deleting needs care. Chaining is the simpler and more forgiving choice when the table may get crowded or when you delete often.
Hash the key and take it mod the table size, exactly as chaining does. That index is the key's home slot.
If the home slot is empty, store the entry there. If it holds the same key, update its value. Otherwise move to the next index (wrapping from the last slot back to 0) and check again. If you come back to where you started, the table is full.
Start at the home slot and step forward. A matching key ends the search. An empty slot also ends it, because the key would have been stored there if it existed.
Just emptying a slot can strand keys further along, since a later search would stop at the new gap. This page fixes that by taking out every entry after the deleted one, up to the next empty slot, and inserting each one again so it lands as close to home as it now can.
The page opens on 7 empty slots. Type apple, banana, cherry and date. Apple hashes to 1 and banana to 3, both free. Cherry also hashes to 3, which banana holds, so it probes to slot 4 and stops there. Date hashes to 4, which cherry has just taken, so date moves on to slot 5. Searching for date checks slot 4 (cherry), then slot 5 (date), two probes. Now delete banana: slot 3 is emptied, then cherry is rehashed from slot 4 back to its home at 3, and date from slot 5 back to its home at 4. The table ends as apple at 1, cherry at 3 and date at 4, with every key back in its home slot.
Keys that collide pile up into runs of filled slots, and any new key that hashes anywhere into a run has to walk to its end, making the run longer still. This is called primary clustering, and it is why linear probing slows down sharply as the table fills. Quadratic probing and double hashing spread the probes out to avoid it.
Many libraries delete by leaving a "deleted" marker that searches walk past and inserts may reuse, instead of moving entries as this page does. Both keep searches correct; markers are cheaper per delete but build up until the table is rebuilt.
Time: while the table is kept well under full, a probe sequence is a few slots long on average; in a nearly full table one long cluster can make a single operation walk all N entries. Space: one array of M slots holds everything, and M must stay larger than the number of keys.