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
. You must modify the input array in-place.
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
.For example:
Input: nums = [1,1,2]
Output: 2
, and nums
becomes [1,2,_]
(where _
represents irrelevant values).
Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5
, and nums
becomes [0,1,2,3,4,_,_,_,_,_]
Constraints:
1 <= nums.length <= 3 * 10^4
-100 <= nums[i] <= 100
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:
The brute force method to remove duplicates means we examine each number one by one, comparing it to all other numbers to find and eliminate the copies. It's like manually checking every item against every other item to ensure uniqueness. It's straightforward but can be slow when dealing with many numbers.
Here's how the algorithm would work step-by-step:
def remove_duplicates_brute_force(numbers):
duplicate_indices = []
for current_index in range(len(numbers)):
# Compare the current number with all subsequent numbers
for comparison_index in range(current_index + 1, len(numbers)):
if numbers[current_index] == numbers[comparison_index]:
# Mark the comparison number as a duplicate
duplicate_indices.append(comparison_index)
# Create a new list to store non-duplicate numbers
unique_numbers = []
for index in range(len(numbers)):
# Append only non-duplicate numbers to the new list
if index not in duplicate_indices:
# Only add if it's not a duplicate
unique_numbers.append(numbers[index])
return unique_numbers
The key insight is that since the list is already sorted, identical values will be right next to each other. We can take advantage of this order to quickly identify and replace the duplicates. We essentially have two 'pointers': one to track the next available spot for a unique element, and another to scan the list.
Here's how the algorithm would work step-by-step:
def remove_duplicates_from_sorted_array(numbers):
if not numbers:
return 0
# Index to track the next unique element's position
next_unique_index = 1
# Iterate through the array to find unique elements
for current_index in range(1, len(numbers)):
# Compare current element with the element before next_unique_index
if numbers[current_index] != numbers[next_unique_index - 1]:
# Found a new unique number.
numbers[next_unique_index] = numbers[current_index]
# Move the next unique index forward.
next_unique_index += 1
# The first 'next_unique_index' elements are now unique.
return next_unique_index
Case | How to Handle |
---|---|
Null or empty input array | Return 0 indicating no unique elements or throw an IllegalArgumentException if null is not permitted by the problem. |
Array with a single element | Return 1 since the single element is inherently unique. |
Array with all identical elements | The algorithm should correctly reduce the array to a single instance of that element and return 1. |
Array with a large range of integer values (including negative and positive) | Ensure the algorithm handles extreme integer values without causing overflow or underflow issues. |
Array with maximum possible size | Verify the algorithm's space complexity doesn't exceed allowed memory constraints for large arrays. |
Array already containing only unique elements | The algorithm should correctly identify that all elements are unique and return the original array's length. |
Array with duplicates clustered together at the beginning, middle, or end | The algorithm should correctly handle different distributions of duplicates within the array. |
Array containing only negative numbers | Verify that the algorithm correctly handles negative numbers without issues related to sign comparison. |