Why is the key being stored with the value?


#1

Question

In the context of this exercise, why is the key being stored along with the value?

Answer

In order to implement open addressing, we need to store the keys along with the values in our hash map.

When we add a key-value, the key either already exists in the hash map, or it does not. By storing the keys, we can compare each of them with our added key. If the key already exists, we would overwrite the current value, and if not, we will add the new key-value pair to the hash map.


#2

If we implement separate chaining to handle collisions instead of open addressing, we also need to store each key with its associated value, since more than one key-value pair can get stored in each bucket.