awelgorithms

Recognize a key-value query

Updated

A key-value query names a key and wants the payload: count, index, group, weight, last-seen time. If you can say the thing stored for x, you want a map.

What to focus on

If you can only say is x here, that is a set. If you say the item at position i, that is a list. Draw the pairs before coding.

Worked examples

count

a = [4, 1, 9, 1], how many 1s
  1. key 1, value 2

Result: map

membership

a = [4, 1, 9, 1], is 8 present
  1. no payload

Result: set

position

a = [4, 1, 9], item at index 1
  1. index is already the key

Result: list, a[1]=1

Try it

Write chooseStructure(a, question). a is an array of integers. question is what you need. Return "map" when you need a payload stored for x, "set" for in-or-out, "list" for item at index i. a = [4, 1, 9, 1], how many 1s → map. a = [4, 1, 9, 1], is 8 present → set. a = [4, 1, 9], item at index 1 → list, a[1]=1.

a is a non-empty list of integers.