Learn
Hash Maps: Python
Handling Collisions in the Getter

When we retrieve hash map values, we also need to be aware of the fact that two keys could point to the same array index.

Instructions

1.

In our .retrieve() method, after finding the array index, we want to check to make sure that the index corresponds to the key we’re looking for.

Save the array value at our compressed hash code into possible_return_value.

2.

Instead of just returning the array’s contents at that index, check if possible_return_value is None. If so, return None.

3.

If possible_return_value is not None, check if the first element in possible_return_value (index 0) is the same as key.

If so, return possible_return_value[1], the value.

4.

If our current array value doesn’t contain the key we’re getting, we’ll need to use open addressing to find the next place where the key will be. We’ll be doing that soon!

Folder Icon

Sign up to start coding

Already have an account?