values is sorted in non-decreasing order. Keep every value at most twice by dropping the extra copies right inside this array, without making a copy.
Let k be the number of elements left after the cleanup. Modify values so that its first k elements hold the result in the original order; whatever follows does not matter.
Return k.
Try to do it in one pass with O(1) extra memory.values=[3, 3, 3, 3, 8, 9, 9] → 5
Two of the four 3s stay. The function returns 5 and the array starts with [3, 3, 8, 9, 9, …].
values=[-4, 0, 0, 2] → 4
No value occurs more than twice, so nothing changes: the answer is 4.
values=[6, 6, 6] → 2
values is sorted in non-decreasing ordervalues=[3, 3, 3, 3, 8, 9, 9]
5
Run checks the first 3 tests in your browser. Submit checks all 131 tests on the server, hidden ones included.