awelgorithms

Remove the last map entry

Updated

Deleting the only remaining key yields {}. That is a normal empty map, not a destroyed object. Further gets miss. A later put starts over at size 1.

What to focus on

Do not leave a tombstone that still counts toward size. Empty means size 0.

Worked examples

last of one

a = [7] as {7:3}, delete 7
  1. size 1→0

Result: {}

last of three

a = [4, 1, 9] as {4:2,1:8,9:3}, delete 4, delete 1, delete 9
  1. three deletes

Result: {}

already empty

a = []
  1. delete anything is no-op

Result: {}

Try it

Write remove(m, k). m is the hash map. k is a present key. Deleting the last key yields {}. Size is 0. A later put starts at size 1. a = [7] as {7:3}, delete 7 → {}. a = [4, 1, 9] as {4:2,1:8,9:3}, delete 4, delete 1, delete 9 → {}. a = [] → {}.

keys are integers. Each named delete is a present key. A later put onto {} is in scope.