The problem requires removing duplicates from a sorted array in-place, such that each unique element appears only once, and returning the length of the resulting array. Since the array is sorted, identical elements are adjacent, which we can leverage to identify duplicates efficiently. The solution uses a two-pointer approach to maintain a subarray of unique elements at the start of the array. Here’s how it works:
j
to 1, representing the position where the next unique element should be placed. The first element at index 0 is always unique, so we start checking from index 1.i
, we compare it with the previous element (nums[i-1]
). If they differ, nums[i]
is a new unique element.nums[i] != nums[i-1]
), we place it at index j
by setting nums[j] = nums[i]
and increment j
. This ensures that the subarray from index 0 to j-1
contains only unique elements in their original order.j
represents the number of unique elements, and the first j
elements of nums
contain the unique elements in sorted order. We return j
as the new array length.j
to track the position for the next unique element allows us to modify the array in-place efficiently, without needing extra space.
The goal of this problem is to remove duplicate entries from a sorted array nums
such that each element appears only once and return the new length of the modified array. Importantly, this must be done in-place without allocating additional space. Since the array is sorted, duplicates are guaranteed to be adjacent, simplifying the comparison process.
To solve this efficiently, we use the two-pointer technique. One pointer (i
) scans through the array, and another pointer (j
) keeps track of the position where the next unique value should be inserted. Initially, j
is set to 1, since the first element is always unique by default. We then start iterating from the second element (index 1).
During each iteration, if the current element nums[i]
is not equal to nums[i - 1]
, it means we have encountered a new unique value. In that case, we store this new value at the j
index and increment j
by 1. This ensures that the subarray nums[0..j-1]
always contains the unique values in their original relative order.
For input [1, 1, 2, 2, 3]
, the pointer i
moves through each index, and whenever it finds a new value different from the one before it, that value is placed at position j
. At the end of the process, nums
will look like [1, 2, 3, _, _]
, and the function will return 3
.
This algorithm operates in linear time O(n), where n is the number of elements in the input array. It also satisfies the space constraint, as no additional data structures are used — only pointers within the original array.
The key advantage of this method is its efficiency in both time and space. The sorted property ensures that all duplicates are adjacent, making it easy to compare current and previous elements. The two-pointer technique enables in-place modification while preserving the input's structure and constraints. It’s a classic example of space-efficient array manipulation commonly asked in technical interviews.