Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same. Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. Return k after placing the final result in the first k slots of nums.
Example 1
Input: nums = [1,1,1,2,2,3]
Output: [1,1,2,2,3]
Explanation: Each value may appear at most twice, so the third 1 is dropped: [1,1,2,2,3].
Example 2
Input: nums = [0,0,1,1,1,1,2,3,3]
Output: [0,0,1,1,2,3,3]
Explanation: The value 1 appears four times, so the last two copies are dropped. Everything else already appears at most twice, giving [0,0,1,1,2,3,3].
Example 3
Input: nums = [1]
Output: [1]
Explanation: A single element is kept as is.
Keep a write index k. Accept the current value if we've written fewer than two elements, or if it differs from the value two slots back (nums[k-2]) — that guarantees at most two copies of any value.
Run checks all cases above. Submit evaluates all test cases.