Remove Duplicates
Array, Two Pointers
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:
Change the array 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
. Return k
.
Solutions
The key to solving this problem is understanding that you’re working with a sorted array, which means all duplicates of a particular number are adjacent to each other. This property allows you to effectively remove duplicates by comparing each element with its next one. The in-place requirement means you should not use additional space proportional to the size of the input array. You can use a constant amount of extra space, though. The relative order of the elements should be kept the same, meaning that you should not sort the array or change the order of unique elements. Finally, you should return the number of unique elements after removing duplicates. The remaining elements of the array after the first k
are not important.
Approach - Brute Force Technique
This approach uses a temporary array to store the unique elements from the original array. It iterates through the original array and checks if the current element is different from the next one. If it is, the current element is stored in the temporary array. After the iteration, the last element of the original array is also stored in the temporary array. Finally, the unique elements from the temporary array are copied back to the original array. The function returns the number of unique elements.
Steps
Empty Array Check: If the length of
nums
is 0, the method returns 0. This is because there are no elements to process in an empty array.Temporary Array Initialization: A temporary array
temp
of the same length asnums
is created. This array is used to store the unique elements fromnums
.Variable Initialization: A variable
j
is initialized to 0. This variable is used as an index for thetemp
array.Iteration Over
nums
: The method iterates over thenums
array from the first element to the second last element. For each element at indexi
, it checks if it is different from the next element at indexi + 1
.Storing Unique Elements: If the current element is different from the next one, it means that the current element is unique and it is stored in the
temp
array at indexj
. Then,j
is incremented by 1.Storing the Last Element: After the iteration, the last element of
nums
is stored intemp
at indexj
, andj
is incremented by 1. This is done because the last element is not checked in the iteration.Copying Back to
nums
: The unique elements fromtemp
are copied back tonums
from index 0 toj - 1
.Return Value: The method returns
j
, which is the number of unique elements innums
.
Complexity
Time complexity:
O(N)
Space complexity:
O(N)
Approach - Two Pointers
This approach avoids the use of extra space by maintaining a pointer that keeps track of the position of the last unique element in the original array. It iterates through the original array and checks if the current element is different from the element at the pointer’s position. If it is, the pointer is moved one step forward and the current element is stored at the pointer’s position in the original array. The function returns the number of unique elements.
Steps
Check for Empty Array: The function first checks if the input array
nums
is empty. If it is, it immediately returns 0, as there are no elements and hence no duplicates.Initialization: A variable
i
is initialized to 0. This variable will serve as a pointer to the last non-duplicate element in the array.Iteration: The function then enters a loop that starts from the second element of the array (index 1) and goes up to the last element. The loop variable
j
serves as a pointer to the current element being checked.Check for Duplicates: Inside the loop, the function checks if the current element
nums[j]
is different from the last non-duplicate elementnums[i]
. If it is, it meansnums[j]
is not a duplicate of the last non-duplicate element.Update Non-Duplicate Element: If
nums[j]
is not a duplicate, the function incrementsi
and updatesnums[i]
tonums[j]
. This effectively moves the non-duplicate element to the next position in the array, right after the last non-duplicate element.Return Result: After the loop finishes, the function returns
i + 1
, which is the number of non-duplicate elements in the array.
Complexity
Time complexity:
O(N)
Space complexity:
O(1)
Last updated