awelgorithms

Values without keys is a list

Updated

A sequence with order, duplicates, and positions is a list. Wrapping it as {0:a[0], 1:a[1], …} is a slower list: get-by-index was already O(1), and you paid hashing to pretend indices are keys.

What to focus on

Use a map when the lookup key is a value or an id, not the position you already have. The 2nd item is a[1]. The item whose id is 9 is a map.

Worked examples

order matters

a = [4, 1, 9]
  1. index 0 is 4
  2. walk in order

Result: list

lookup by id

a = [4, 1, 9], ids are the values, need id 9
  1. key 9 → maybe index 2

Result: map

empty

a = []
  1. empty list vs empty map

Result: [] is the sequence, {} is no pairs

Try it

Write chooseStore(a, need). a is an array of integers. need is "order", "id", or "keep dups". Return "list" when order, index, or both copies of a duplicate matter. Return "map" only to answer where an id lives. a = [4, 1, 9] → list. a = [4, 1, 9], ids are the values, need id 9 → map. a = [] → [] is the sequence, {} is no pairs.

a is a list of integers and may be empty.