Remove Duplicates from Sorted Array

Easy

Topics
ArrayTwo Pointers

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

101122first 2 slots
kept
Sorted input means duplicates are neighbours, so one comparison per element decides it. A write pointer that only advances on a new value compacts the kept values into the front of the array in place; the answer is the count, 2, and whatever sits past it is ignored.
Cells are the array values; the small numbers underneath are indices.
Input:  nums = [1,1,2]
Output: 2

Example 2

00011213142526373849first 5 slots
kept
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

  1. k = 0, the position for the next unique value.
  2. For each value: if k == 0 or it differs from nums[k-1], write it at k and increment.
  3. 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,4

Complexity

  • 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 than nums[k-1] among the kept values. On this problem they coincide because the input is sorted, but the k-1 form 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 k entries in place.
  • Trying to delete elements, which is O(n) per removal and O(n^2) overall.
  • Forgetting the k == 0 case, which reads nums[-1].
  • Assuming the tail beyond k must 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 k
SensAI
Beta
Tap the mic to stop voice mode