We have a hash table implementation, but what happens when two different keys generate the same index? Run the code in HashTable.swift to see a collision in action.
Instead of returning "Marsh Plant"
and "Wild Animal"
, we retrieve "Wild Animal"
twice. This is because both key-value pairs are assigned to the same index 0 and the first value, "Marsh Plant"
was overwritten.
When two different keys resolve to the same array index, this is called a collision. In our current implementation, all keys that resolve to the same index are treated as if they are the same key. This is a problem because they will overwrite one another’s values.
Instructions
Run the code in the text editor to see the result of a collision between two keys and notice that the value "reed"
and "deer"
both return "Wild Animal"
.