#80
Remove Duplicates from Sorted Array II
specialist · 660 · lc medium +30 · verified · 64.3% accepted · 8,200 likes · top 68%
Description
Non-decreasing integer array nums may contain duplicates. Remove excess duplicates in-place so each unique value appears at most twice. Return k, the count of valid elements. The first k entries of nums must hold the result with no extra array allocated.
Example 1:
int[] nums = [...]; // Input array
int[] expectedNums = [...]; // The expected answer with correct length
Example 2:
int k = removeDuplicates(nums); // Calls your implementation
Example 3:
assert k == expectedNums.length;
for (int i = 0; i < k; i++) {
assert nums[i] == expectedNums[i];
}
Example 4:
Input: nums = [1,1,1,2,2,3]
Output: 5, nums = [1,1,2,2,3,_]
Explanation: Your function should return k = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).
Example 5:
Input: nums = [0,0,1,1,1,1,2,3,3]
Output: 7, nums = [0,0,1,1,2,3,3,_,_]
Explanation: Your function should return k = 7, with the first seven elements of nums being 0, 0, 1, 1, 2, 3 and 3 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).
Code
1
2
3