awelgorithms

Decide map versus set

Updated

Payload means map. Membership or uniqueness means set. Indices, counts, lists-under-keys are payloads. Dummy boolean values are a set. If you start with a set and later need an index, you will rebuild a map. Start with the map.

What to focus on

Two-sum existence: set. Two-sum indices: map. Dedup: set. Dedup but keep first index: map.

Worked examples

indices

a = [4, 1, 9], t = 10, return two indices
  1. need where 1 lives

Result: map

exists

a = [4, 1, 9], t = 10, return true/false
  1. need whether 1 was seen

Result: set

empty

a = [], t = 0
  1. no pair

Result: either, both empty

Try it

Write chooseStructure(a, t, want). a is an array of integers. t is the two-sum target. want is "indices", "exists", or "dummyTrue". Return "map" for indices. Return "set" for existence or dummy true. Do not store true as a fake payload. a = [4, 1, 9], t = 10, return two indices → map. a = [4, 1, 9], t = 10, return true/false → set. a = [], t = 0 → either, both empty.

a is a list of integers and may be empty.