Now lets use our open addressing scheme in the setter for our HashMap.



Now that we have a hash function that uses the number of collisions to determine the hash code, we can update where we set a key in the event of a collision.

When we notice that the key we’re trying to set is different from the key at our hash code’s address, create a new variable called number_collisions, set that equal to 1.


After defining number_collisions, create a new while loop that checks if current_array_value[0] != key.


In the while loop, you want to replicate our setting logic while incrementing the number of collisions.

Call .hash() with both the key and number_collisions. Save that result into new_hash_code.


Plug new_hash_code into .compressor(). Save that result into new_array_index.


Check self.array at new_array_index and save the result as current_array_value. Check against the three possibilities:

  • If it’s None, save the [key, value] at self.array[new_array_index] and then return.
  • If it has a value, but the same key as key, overwrite the array at that address with [key, value] and then return.
  • If it has a value, but a different key, increment number_collisions.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?