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. It does not matter what you leave beyond the first k
elements.
Return k
after placing the final result in the first k
slots of nums
.
Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.
Custom Judge:
The judge will test your solution with the following code:
int[] nums = [...]; // Input array int[] expectedNums = [...]; // The expected answer with correct length int k = removeDuplicates(nums); // Calls your implementation assert k == expectedNums.length; for (int i = 0; i < k; i++) { assert nums[i] == expectedNums[i]; }
If all assertions pass, then your solution will be accepted.
Example 1:
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 2:
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).
Constraints:
1 <= nums.length <= 3 * 104
-104 <= nums[i] <= 104
nums
is sorted in non-decreasing order.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a list of numbers sorted from smallest to largest, and you want to remove duplicates but allow each number to appear at most twice. The brute force strategy checks every possible combination of numbers, keeping only those that meet the 'at most twice' rule for each number.
Here's how the algorithm would work step-by-step:
def remove_duplicates_brute_force(numbers):
new_list = []
for number in numbers:
# Check frequency of number in the new list
frequency = new_list.count(number)
# Add the number if it appears less than twice
if frequency < 2:
new_list.append(number)
return new_list
The goal is to modify the existing sequence so that no number appears more than twice, keeping the sequence sorted. The clever shortcut is to overwrite the older part of the sequence with the new, corrected one as you go, using only a small, fixed amount of extra memory.
Here's how the algorithm would work step-by-step:
def remove_duplicates(numbers):
if not numbers:
return 0
# Use insert_position to track the index for the next unique element.
insert_position = 0
for number in numbers:
# Check if the current number is a duplicate.
if insert_position < 2 or number != numbers[insert_position - 2]:
# Overwrite the array at insert_position with the current number.
numbers[insert_position] = number
# Only increment if we've placed a unique number
insert_position += 1
return insert_position
Case | How to Handle |
---|---|
Empty input array | Return 0 immediately, as there are no elements to process. |
Null input array | Throw an IllegalArgumentException or return 0 after validating the argument to prevent NullPointerException. |
Array with one element | Return 1, as a single element always satisfies the condition. |
Array with two elements that are different | Return 2, as two different elements also satisfy the condition. |
Array with two elements that are the same | Return 2, as the same elements can appear at most twice. |
Array with all elements identical | The solution should correctly condense the array to have only two occurrences of the repeated number and return 2. |
Array with negative numbers, positive numbers, and zeros | The solution should correctly handle the negative numbers, positive numbers, and zeros while maintaining the sorted order and limiting duplicates. |
Array with a mix of elements where some are repeated more than twice, and some appear once or twice | The algorithm must correctly filter those appearing more than twice, maintaining correct ordering and limiting the total count. |