Is there a method to remove key-values from a hash map?

Question

In the context of this lesson, where we implement a hash map, is there a method to remove key-values from a hash map?

Answer

Yes, although we will not be implementing a method to remove a key-value entry in this lesson, there are many ways you can accomplish this.

Some programming languages that include an implementation of a hash map, will usually have a built-in method to remove entries. For example, in Java, the HashMap class has a built-in method called remove(), that removes the mapping of any specified key from the hash map.

Another way of removing entries in a hash map is called “Lazy Deletion”, which essentially marks the entry as deleted, has all its methods treat that entry as deleted, but does not actually remove it entirely until later. One reason for this method is because of the costs to remove an entry, which can slow down other operations.

To implement your own remove method, try to utilize the implementations of the assign and retrieve methods. The main idea would be to somehow “remove” a key-value, without changing the number of indices in the array, meaning a method like pop() would not work.