Limit duplicates to two

Write codeMedium13 min

Your task

Given a sorted integer array nums, produce a version where each distinct value appears at most twice, keeping the sorted order and using a single pass with a write pointer (no counting map). Return the trimmed array. Example: [60, 60, 60, 62, 62, 65] becomes [60, 60, 62, 62, 65]. Think about what single comparison tells you whether the current element may be kept.

Examples

cap_repeats_at_two(nums: list[int]) -> list[int]
Example 1
Inputcap_repeats_at_two([60, 60, 60, 62, 62, 65])
Output[60, 60, 62, 62, 65]