Keep last occurrences
Given a list of integers that may contain repeats, produce a new list that keeps only the last occurrence of each distinct value, with the survivors appearing in the same relative order they hold in the original list. For example, [1, 2, 1, 3, 2] keeps the 1 at index 2, the 3 at index 3, and the 2 at index 4, so the answer is [1, 3, 2]. Note this is not the same as keeping first occurrences and cannot be solved by a single forward filter without preparation.
Implement
keep_last_occurrences(nums: list[int]) → list[int]Examples
in
[[1,2,1,3,2]]out[1,3,2]What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 14 min
solution.py
InputExpectedGot
[[1,2,1,3,2]][1,3,2]not run yetsample