Code RoomLimit duplicates to two
MediumPrep Room Coding #610

Limit duplicates to two

CodingAlgorithms & data structuresEntry–Mid~13 min

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.

Implement
cap_repeats_at_two(nums: list[int]) → list[int]
Examples
in[[60,60,60,62,62,65]]out[60,60,62,62,65]
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 13 min
InputExpectedGot
[[60,60,60,62,62,65]][60,60,62,62,65]not run yetsample