Remove Duplicates from Sorted Array
An easy Two Pointers problem included in Apna College, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- Two Pointers
- Sheets
- 2
- Core for
- 10 roles
- Platform
- LeetCode
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 = [1, 1, 2]
- Output
- 2, nums = [1, 2]
- Why
- The first two elements are unique. The function returns length 2.
Example 2
- Input
- nums = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]
- Output
- 5, nums = [0, 1, 2, 3, 4]
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-09In 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.
What each operation costs
| Operation | Time |
|---|---|
| converging scan across sorted collection | O(n) |
| slow and fast cycle traversal | O(n) |
| auxiliary memory overhead for pointer markers | O(1) |
What usually goes wrong with Two Pointers
- Applying convergent opposite-end pointers to an unsorted collection without realizing that sorting was a necessary precondition for direction-based pruning.
- Allowing pointers to cross past one another or crash into identical indices when matching duplicate values, skipping the termination condition.
- Forgetting to advance pointers past repeated values during duplicate-skipping loops, creating unexpected infinite iterations on matching elements.
Which roles need this problem
Two Pointers is a core topic for these 10 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 15 more roles, including SDE / Backend Engineer, ML Engineer, Graphics Engineer.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore Two Pointers problems
Problem set and role mapping as of .