Remove Duplicates from Sorted Array
An easy Two Pointers interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Two Pointers
- Sheets
- 2
- Core for
- 10 roles
The problem
Given a sorted array, remove duplicate elements in place so each element appears only once and return the new length.
Example 1
- Input
- nums = [3, 3, 5]
- Output
- 2, nums = [3, 5]
- Why
- The duplicate 3 is dropped, leaving two unique values.
Example 2
- Input
- nums = [1, 1, 2, 4, 4, 4, 6, 7, 7]
- Output
- 5, nums = [1, 2, 4, 6, 7]
Constraints
- 1 <= nums.length <= 3 * 10^4
- -10^4 <= nums[i] <= 10^4
- nums is sorted in non-decreasing order
How to think about it
Updated 2026-09-29In a sorted array, identical numbers are always contiguous. The first element of any run is guaranteed to be unique, so an explorer pointer only needs to check whether the current number differs from the last committed value before placing it at the boundary.
Approaches, worst first
Splice or shift in place
time O(n^2) · space O(1)
Iterate through the array and erase duplicate elements by shifting all later elements left. Every removal shifts up to n elements, making the run quadratic on arrays containing many duplicates.
Fast and slow write pointersWrite this one
time O(n) · space O(1)
Keep a write pointer at index 0. Advance a read pointer from index 1 to n-1. Whenever `nums[read] != nums[write]`, increment write and assign `nums[write] = nums[read]`. The unique prefix is compacted in one pass.
Where people lose marks · 3
- Returning the write index instead of the count. Because the write pointer is 0-indexed, the total count of unique elements is `write + 1`.
- Out-of-bounds index reads on an array of length 1. Starting comparison against an uninitialized or shifted index breaks single-element inputs.
- Comparing `nums[read]` against `nums[read - 1]` instead of `nums[write]`. While valid for consecutive duplicates, referencing the committed element `nums[write]` keeps the invariant clearer and prevents stale comparison errors.
The theory behind it
Two Pointers — the ground this problem stands on. All Two Pointers problems
What Two Pointers is
Two pointers is a search coordinated by two fingers placed on different entries of an ordered row, stepping inward toward each other or advancing at different speeds. Instead of scanning with nested loops that re-examine identical pairs, each pointer moves unidirectionally based on comparisons. Because every movement permanently prunes bad candidate combinations, the search inspects the collection in a single joint pass.
When to reach for it
Reach for two pointers when problems require finding index pairs, reversing sequences, trapping rainwater, or filtering duplicates in sorted arrays. When an input is already sorted and an exhaustive search takes quadratic time, opposing pointers meeting in the center often solve it linearly. Fast and slow pointer variations also detect cycles or locate midpoint nodes across linked sequences.
How the pattern works
Initialize pointers at opposing edges for convergent searches, or start both at the beginning to move at different speeds. Under a convergent setup, calculate current pair metrics; if the sum falls short of your target, advance the lower pointer rightward to increase value, and if it exceeds your target, shift the upper pointer leftward to decrease value. Each pointer move throws away only pairs that were already known to be wrong, so the right answer is never missed.
More Two Pointers problems
Target Roles
Core requirement for 10 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .