Remove elements from a map

To remove an element from a map, use the remove() method. The remove method takes one parameter, which is the map key.

Removing a key value pair from map

Original Map

Initial key-value pairs

France
Paris
Japan
Tokyo
Australia
Canberra

Remove Key

Removing a key using map remove() method.

Japan

Result

The final map after removing a key value pair from the map.

France
Paris
Australia
Canberra

The following is an example of a map containing products and their prices. Using the remove() method, we remove the item 'Ipad' from the map.

Loading...

In the above example:

  • The map key is a string and value is an integer.
  • The remove() method is used to remove an element from the map.
  • The remove() method accepts the key as input. If the key is found, the corresponding element will be removed from the map.

Try It Yourself

Loading...