Remove Duplicates from Sorted Array
Remove repeated values from a sorted array in place, preserving its increasing order. Track the length of the unique prefix.
Work through the cases
3 examplesInput notation
A nonempty, nondecreasing line of space-separated integers.Expected result
The unique values separated by spaces.Case 1
Input
1 1 2 4 4
Expected output
1 2 4
Case 2
Input
5 5 5
Expected output
5
Case 3
Input
-3 -3 -1 0
Expected output
-3 -1 0
These notes use simplified input/output notation. The linked platform defines the method signature and judge format.
FROM IDEA TO IMPLEMENTATION
Understand the approach.
Compare the documented approaches below. Brute force, better, and optimal versions appear where available; a direct exercise may need only one approach.
The first idea is a hash set of seen values, then write them back. n ≤ 3 × 10^4 would pass, but the problem wants an in-place rewrite, so the extra table is wasteful. The bottleneck is the "have we seen this" query. The array is non-decreasing, so equals sit next to each other and a global lookup is unnecessary. Compare x with the last value already written. If it differs (or nothing has been written yet), write it at index k; otherwise skip. k is both the write pointer and the unique length. One pass, O(1) extra space.
We use a variable k to record the current length of the processed array. Initially, k=0 represents an empty array.
Then we traverse the array from left to right. For each element x we encounter, if k=0 or x \neq nums[k-1], we place x in the position of nums[k], and then increment k by 1. Otherwise, x is the same as nums[k-1], so we skip this element. Continue to traverse until the entire array is traversed.
In this way, when the traversal ends, the first k elements in nums are the answer we are looking for, and k is the length of the answer.
The time complexity is O(n), and the space complexity is O(1). Here, n is the length of the array.
Supplement:
The original problem requires that the same number appear at most once. We can extend it to keep at most k identical numbers.
- Since the same number can be kept at most k times, we can directly keep the first k elements of the original array; - For the following numbers, the premise of being able to keep them is: the current number x is compared with the last kth element of the previously retained numbers. If they are different, keep them, otherwise skip them.
Similar problems:
- 80. Remove Duplicates from Sorted Array II
class Solution {
public int removeDuplicates(int[] nums) {
int k = 0;
for (int x : nums) {
if (k == 0 || x != nums[k - 1]) {
nums[k++] = x;
}
}
return k;
}
}Reference code. Platform solutions may use supplied node types, imports, or collection libraries; foundational examples are complete programs.
Solutions adapted from Doocs / LeetCode contributors, licensed under CC BY-SA 4.0. Formatting changed; TypeScript versions converted to JavaScript. Supplemental implementations and approach notes added. Adapted solution material is available under the same license.