Remove Duplicates from Sorted Array
Easy
Topics
Given a sorted integer array nums, remove the duplicates in-place so each unique element appears once, and return the number of unique elements k.
Example 1
Input: nums = [1,1,2] Output: 2
Example 2
Input: nums = [0,0,1,1,1,2,2,3,3,4] Output: 5
Constraints
- 1 <= nums.length <= 3*10^4
- nums is sorted in non-decreasing order.
Editorial — intuition, approach, complexity and pitfalls
Intuition
The array is sorted, so duplicates are adjacent. We need the unique values packed into the front and their count returned, using O(1) extra space — so the work has to happen in place.
This is the slow/fast two-pointer pattern for in-place compaction. One index marks where the next kept value goes; the other scans. A value is kept when it differs from the last one kept, which for sorted input is the same as differing from its predecessor.
The write index never overtakes the read index, so writing can never clobber a value still to be read — that is what makes in-place compaction safe in general.
Approach
k = 0, the position for the next unique value.- For each value: if
k == 0or it differs fromnums[k-1], write it atkand increment. - Return
k.
Dry Run
nums = [0,0,1,1,1,2,2,3,3,4]
x=0 k==0 write at 0, k=1 [0,...]
x=0 == nums[0] skip
x=1 != nums[0] write at 1, k=2 [0,1,...]
x=1 == nums[1] skip
x=1 == nums[1] skip
x=2 != nums[1] write at 2, k=3 [0,1,2,...]
x=2 == nums[2] skip
x=3 != nums[2] write at 3, k=4 [0,1,2,3,...]
x=3 == nums[3] skip
x=4 != nums[3] write at 4, k=5 [0,1,2,3,4,...]
return 5, with the first five slots holding 0,1,2,3,4Complexity
- Time — O(n): one pass.
- Space — O(1): one index. Building a new list of uniques, or using a set, is O(n) and also loses the ordering guarantee a set would scramble.
Common Pitfalls
- Comparing against
nums[i-1]in the original array rather thannums[k-1]among the kept values. On this problem they coincide because the input is sorted, but thek-1form is the one that generalises — and it is what makes "keep at most two duplicates" a one-line change. - Returning the array instead of the count. The judge inspects the first
kentries in place. - Trying to delete elements, which is O(n) per removal and O(n^2) overall.
- Forgetting the
k == 0case, which readsnums[-1]. - Assuming the tail beyond
kmust be cleared. It does not matter what it holds.
Full Solution — all languages
class Solution:
def removeDuplicates(self, nums):
k = 0
for x in nums:
if k == 0 or x != nums[k - 1]:
nums[k] = x
k += 1
return kimport java.util.*;
class Solution {
public int removeDuplicates(int[] nums) {
int k = 0;
for (int x : nums) {
if (k == 0 || x != nums[k - 1]) {
nums[k] = x;
k++;
}
}
return k;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int removeDuplicates(vector<int>& nums) {
int k = 0;
for (int x : nums) {
if (k == 0 || x != nums[k - 1]) {
nums[k] = x;
k++;
}
}
return k;
}
};/**
* @param {number[]} nums
* @return {number}
*/ var removeDuplicates = function(nums) {
let k = 0;
for (const x of nums) {
if (k === 0 || x !== nums[k - 1]) {
nums[k] = x;
k++;
}
}
return k;
};