Map a value to its last index
Updated
Left-to-right put (always overwrite) stores the last index. Right-to-left put-if-absent does the same. Pick one direction and stick to it.
What to focus on
Last index is what you want for rightmost 1 or for some window bounds. It is not first-seen.
Worked examples
repeat last
a = [4, 1, 9, 1]- 1→1 then 1→3
Result: {4:0, 1:3, 9:2}
unique
a = [4, 1, 9]- first equals last
Result: {4:0, 1:1, 9:2}
empty
a = []- no indices
Result: {}
Try it
Write lastIndex(a). a is an array of integers. Always overwrite value→index left to right. Return the map. a = [4, 1, 9, 1] → {4:0, 1:3, 9:2}. a = [4, 1, 9] → {4:0, 1:1, 9:2}. a = [] → {}.
a is a list of integers and may be empty.