awelgorithms

Count entries in a hash map

Updated

Size is how many keys are stored, not the sum of values and not the last key. Put of a new key +1. Put of an existing key does not change size. Delete of a present key −1.

What to focus on

Size 3 with keys 4,1,9 is three entries even if values are huge. Size is not get(key).

Worked examples

three keys

a = [4, 1, 9] each put once
  1. three distinct keys

Result: size 3

overwrite

a = [4, 1, 9], put 1→99 again
  1. 1 already a key
  2. size stays 3

Result: size 3

empty

a = []
  1. no keys

Result: size 0

Try it

Write size(m). m is the hash map of key to value. Return how many keys are stored. Overwrite does not change size. Delete of a present key drops size by 1. a = [4, 1, 9] each put once → size 3. a = [4, 1, 9], put 1→99 again → size 3. a = [] → size 0.

keys are integers. Empty is in the cases. Deletes named in the drill are present keys.