Given an integer array nums sorted in non-decreasing order, remove duplicates in place so that each distinct value appears at most twice, keeping the relative order. Put the kept values in the first k slots of nums and return k. Use O(1) extra memory.
Given an integer array nums sorted in non-decreasing order, remove duplicates in place so that each distinct value appears at most twice, keeping the relative order. Put the kept values in the first k slots of nums and return k. Use O(1) extra memory.
Example 1:
Input: {"nums":[1,1,1,2,2,3]}
Output: [1,1,2,2,3]
Constraints:
1≤nums.length≤3×104
−104≤nums[i]≤104
nums is sorted in non-decreasing order.
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"nums":[1,1,1,2,2,3]}
k = 0 marks the end of the kept prefix. Every value may appear at most twice in it, so each new value is checked against nums[k - 2].