AlgoMaster Logo

Remove Duplicates from Sorted Array

Last Updated: March 31, 2026

easy
3 min read

Understanding the Problem

At first glance, removing duplicates from a sorted array seems trivial: just pick one of each value. But the in-place constraint makes it interesting. We can't create a new array and copy unique values over (well, we can, but that wastes space). We need to rearrange the original array so that unique elements sit at the front, and then tell the caller how many there are.

The fact that the array is already sorted is a huge advantage. All duplicates are grouped together, sitting right next to each other. So we never need to search the whole array to check if we've seen a value before. We just need to compare each element with the one before it (or the last unique one we placed). This observation is what makes the problem solvable in a single pass.

Key Constraints:

  • 1 <= nums.length <= 3 * 10^4 → With n up to 30,000, O(n^2) means about 900 million operations, which is borderline. O(n) or O(n log n) is preferred.
  • -100 <= nums[i] <= 100 → The value range is tiny (only 201 possible values). This means we could use a hash set or counting array, but since the array is sorted, we don't need to.
  • nums is sorted in non-decreasing order → This is the most important constraint. Duplicates are adjacent, so a single scan comparing neighbors is enough.

Approach 1: Using a Hash Set

Intuition

The most straightforward way to think about this: use a data structure that automatically ignores duplicates. A hash set does exactly that. We can collect all unique values, sort them (since the output needs to maintain order), and write them back into the array.

This approach ignores the sorted property entirely and uses extra space, so it's not ideal. But it's a clean mental starting point.

Algorithm

  1. Insert all elements from nums into a hash set to get unique values.
  2. Convert the set to a list and sort it.
  3. Write the sorted unique values back into the first k positions of nums.
  4. Return k, the number of unique elements.

Example Walkthrough

Input:

0
0
1
0
2
1
3
1
4
1
5
2
6
2
7
3
8
3
9
4
nums

After collecting unique values into a set and sorting: {0, 1, 2, 3, 4}

Write them back into the first k positions of nums:

0
0
1
1
2
2
3
3
4
4
5
2
6
2
7
3
8
3
9
4
nums

Code

This approach works correctly but wastes space and ignores the sorted property entirely.

Since duplicates are already grouped together in a sorted array, can we just walk through the array once and place each new unique value in the right spot?

Approach 2: Two Pointers (Optimal)

Intuition

Since the array is sorted, duplicates are always adjacent. We don't need a hash set to detect them. We just need to compare each element with the last unique element we placed.

Algorithm

  1. If the array is empty, return 0.
  2. Initialize insertPos = 1 (the first element is always unique).
  3. Iterate through the array starting from index 1.
  4. When nums[i] is different from nums[insertPos - 1], place nums[i] at nums[insertPos] and increment insertPos.
  5. Return insertPos as the count of unique elements.

Example Walkthrough

1Initialize: insertPos=1, i=1. First element is always unique.
0
0
1
insertPos
0
i
2
1
3
1
4
1
5
2
6
2
7
3
8
3
9
4
1/9

Code