awelgorithms

Iterate the keys of a map

Updated

keys() yields each stored key once. It does not yield values. The set of keys is the domain of get.

What to focus on

Do not expect sorted keys. Sorting keys is an extra O(k log k) step you opt into.

Worked examples

three

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

Result: key set {4, 1, 9}

one

a = [7] as {7:3}
  1. one key

Result: {7}

empty

a = []
  1. no keys

Result: {}

Try it

Write keys(m). m is the hash map. Return each stored key once. Do not sort unless asked. Iteration order is unspecified. a = [4, 1, 9] as {4:2,1:8,9:3} → key set {4, 1, 9}. a = [7] as {7:3} → {7}. a = [] → {}.

keys are integers. Empty is in the cases. Iteration order is unspecified.