Keep at most k copies, in place
nums is sorted in non-decreasing order and may be empty, and k ≥ 1. Rewrite it in place so each value appears at most k times, keeping the original order, and return the kept part: the first length elements of nums, where length is how many you kept.
Use O(1) extra space: overwrite nums as you read it instead of building a second list.
Examples
keep_k_copies([4,4,4,6,6,9], 2)→[4,4,6,6,9]keep_k_copies([1,1,2,3,3,3,3], 1)→[1,2,3]keep_k_copies([0,0,0,0], 3)→[0,0,0]