Skip to content
AI360Xpert
Beta
Difficulty: EasyTwo Pointers

Remove Duplicates from Sorted Array

Problem in Plain English

Given an integer array nums sorted in non-decreasing order, remove the duplicates in place so that each unique value appears once at the front of the array while keeping their relative order. Return k, the number of unique elements.

Problem Statement

Given an integer array nums sorted in non-decreasing order, remove the duplicates in place so that each unique element appears only once at the front of the array, keeping the relative order of the elements.

Return k, the number of unique elements. The first k entries of nums must hold those unique elements; the contents beyond index k - 1 do not matter.

Constraints

  • 1 <= nums.length <= 3 * 10^4
  • -100 <= nums[i] <= 100
  • nums is sorted in non-decreasing order.

Examples

Example 1

Input
nums = [1,1,2]
Output
2, nums = [1,2,_]

Two unique values exist, so k = 2 with nums[0] = 1 and nums[1] = 2; the entry after them is irrelevant.

Example 2

Input
nums = [0,0,1,1,1,2,2,3,3,4]
Output
5, nums = [0,1,2,3,4,_,_,_,_,_]

Five unique values exist, so k = 5 with the front of the array reading 0, 1, 2, 3, 4.

Example 3

Input
nums = [7]
Output
1, nums = [7]

The only value is already unique. The read/write loop does no work and returns k = 1.

Example 4

Input
nums = [-1, -1, -1, 0, 0]
Output
2, nums = [-1, 0, _, _, _]

Three copies of -1 and two copies of 0 collapse to two distinct values. Only the prefix [-1, 0] is checked; the remaining three slots may hold any values.

Intuition

Sorted order makes repeated values consecutive. Keep the first value, then copy only the first value of each later run into the next open position. In [1, 1, 2], keep the first 1, skip the second 1, and write 2 into index 1. A read pointer visits the input while a write pointer counts the compacted prefix; the unused tail has no required contents.

Approaches

Using a Set

Solution Details

Reveal Using a Set: intuition, complexity, and code

Hints

Hint 1
Sorted order means every duplicate sits beside its original, so which neighbour reveals a new value?
Hint 2
One pointer could read every slot while a second pointer remembers where the next unique value goes.
Hint 3
The first element needs no test, so both pointers can start at index 1 with left counting unique values.

Edge Cases

  • All entries identical such as [1, 1, 1, 1], where the answer is 1 and only the front slot matters.
  • No duplicates such as [1, 2, 3, 4], where every value is written back onto itself and k = 4.
  • A single-element array such as [7], where the loop never runs and the answer is 1.

Common Mistakes and Interview Tips

  • Comparing nums[right] against nums[left], the write slot, instead of nums[right - 1], the previous read value.
  • Starting both pointers at 0, which treats the first element as its own duplicate and drops it.
  • Allocating a set or list misses the intended constant-extra-space solution even when the copied prefix is correct.
  • Returning the entire array or its original length instead of k fails the judge contract; only the first k slots are checked.
  • Deleting entries while scanning shifts later indices and can skip duplicates or make the scan quadratic.
  • Interview tip: explain the sorted-input assumption and the prefix invariant. An unsorted array does not make adjacent comparison sufficient.

Key Takeaway

On sorted input, a reader pointer plus a writer pointer compacts matching runs in one pass with constant extra space.