Compact with limit two
You are compacting a list of integers with a soft limit: each distinct value may appear at most twice in the output. Scan the list from left to right and keep an element only if its value has been kept fewer than two times so far; drop it otherwise. The relative order of kept elements must match the original list. For example, [1, 1, 1, 2, 2, 3] becomes [1, 1, 2, 2, 3], and [9, 8, 9, 8, 9, 7] becomes [9, 8, 9, 8, 7]. The input is not sorted.
Implement
cap_two_per_value(nums: list[int]) → list[int]Examples
in
[[1,1,1,2,2,3]]out[1,1,2,2,3]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 10 min
solution.py
InputExpectedGot
[[1,1,1,2,2,3]][1,1,2,2,3]not run yetsample