Compact with limit two
Your task
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.
Examples
cap_two_per_value(nums: list[int]) -> list[int]Example 1
Input
cap_two_per_value([1, 1, 1, 2, 2, 3])Output
[1, 1, 2, 2, 3]