Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums.
Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things:
nums such that the first k elements of nums contain the unique elements in the order they were present in nums initially. The remaining elements of nums are not important as well as the size of nums.k.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,2] Output: 2, nums = [1,2,_] Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 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,2,2,3,3,4] Output: 5, nums = [0,1,2,3,4,_,_,_,_,_] Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively. It does not matter what you leave beyond the returned k (hence they are underscores).
Constraints:
1 <= nums.length <= 3 * 104-100 <= nums[i] <= 100nums 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 line of items already arranged in order, and you want to clean it up by throwing away any exact duplicates so that every item is unique. The brute force approach involves looking at every single pair of items one by one to see if they match.
Here's how the algorithm would work step-by-step:
def remove_duplicates(sorted_numbers):
marked_indices = set()
# Compare every possible pair to find duplicates since brute force requires exhaustive checks
for current_index in range(len(sorted_numbers)):
if current_index in marked_indices:
continue
for comparison_index in range(current_index + 1, len(sorted_numbers)):
if sorted_numbers[current_index] == sorted_numbers[comparison_index]:
marked_indices.add(comparison_index)
clean_collection = []
# Collect only unmarked items to build a duplicate-free list in the original order
for index_position in range(len(sorted_numbers)):
if index_position not in marked_indices:
clean_collection.append(sorted_numbers[index_position])
# Mutate the input array in place to meet the standard problem signature requirement
for final_index in range(len(clean_collection)):
sorted_numbers[final_index] = clean_collection[final_index]
return len(clean_collection)The optimal approach uses a tracking method to rewrite the line of items in place, keeping only the unique ones at the front. By having one person scan through all the items and another person mark where the next unique item should go, we avoid needing extra space or doing messy shuffling.
Here's how the algorithm would work step-by-step:
def remove_duplicates(sorted_numbers_list):
if not sorted_numbers_list:
return 0
# The first item is always unique, so the insertion spot starts right after it.
next_unique_spot = 1
for current_index in range(1, len(sorted_numbers_list)):
# We only care when a new item differs from its predecessor to filter duplicates.
if sorted_numbers_list[current_index] != sorted_numbers_list[current_index - 1]:
sorted_numbers_list[next_unique_spot] = sorted_numbers_list[current_index]
next_unique_spot += 1
# We return the count of unique elements, which matches the final insertion index.
return next_unique_spot| Case | How to Handle |
|---|---|
| Empty array or null input | Return zero immediately as there are no elements to process. |
| Array with a single element | Return one since a single element is already unique by definition. |
| Array with all identical elements | The slow pointer stays at index zero while the fast pointer scans through, resulting in a length of one. |
| Array with no duplicates | Both pointers move synchronously through the array, returning the original length unchanged. |
| Array containing negative numbers and zeros | Value comparisons work identically for negative numbers and zeros since the array is already sorted numerically. |
| Extremely large arrays nearing memory or time limits | An O(N) time and O(1) space two-pointer approach processes the array in a single pass without exceeding constraints. |
| Array with alternating duplicates and unique values | The write pointer correctly selectively updates elements in-place to compress the sequence while preserving relative order. |