Given a sorted integer array arr in non-decreasing order, modify arr in-place so that each unique value appears at most twice. The relative order of elements must be preserved. Return the integer k, representing the number of elements in the modified array. The first k elements of arr should contain the result. Do not use extra space beyond O(1).
Example 1
Input: [2,2,2,3,3,4,4,4,4]
Output: 6
Explanation: After removing extra duplicates, arr becomes [2,2,3,3,4,4].
Example 2
Input: [5,5,6,6,6,7]
Output: 5
Explanation: After removing extra duplicates, arr becomes [5,5,6,6,7].
Example 3
Input: [8,8,8,8,8]
Output: 2
Explanation: After removing extra duplicates, arr becomes [8,8].
Constraints
Case 1
Input: [1,1,2,2,2,3,3,3,4]
Expected: 7
Case 2
Input: [0,1,1,1,2,2,2,3,4,4,4]
Expected: 8
Case 3
Input: [-1,-1,-1,0,0,1,1,1,2]
Expected: 7
Case 4
Input: [10,10,10,10,10,10]
Expected: 2
Case 5
Input: [1,2,3,4,5]
Expected: 5