Remove Duplicates from Sorted Array — Problem & Solution
Remove Duplicates from Sorted Array solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.
Remove Duplicates from Sorted Array is an easy coding problem on arrays and two pointers, asked in Morgan Stanley, Myntra, Tech Mahindra and Hulu interviews.
- Difficulty: Easy
- Topics: Arrays, Two Pointers
- Asked at: Morgan Stanley, Myntra, Tech Mahindra, Hulu
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Given an integer array nums sorted in non-decreasing order, remove the duplicates so each unique element appears once, keeping the order. Return the resulting array.
Example 1
Input: nums = [1,1,2]
Output: [1,2]
Example 2
Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: [0,1,2,3,4]
Constraints
1 <= nums.length <= 30-100 <= nums[i] <= 100nums is sorted in non-decreasing order
Hints for Remove Duplicates from Sorted Array
Hint 1
Because the array is sorted, duplicates sit next to each other.
Hint 2
Compare each element with the previous one — keep it only when it differs.
How to solve Remove Duplicates from Sorted Array
Because the array is sorted, equal values are adjacent — so a value is a duplicate exactly when it equals the one just kept. Use a write pointer for the deduplicated prefix and only advance it when a genuinely new value appears.
Approach
- Keep the first element; set
write = 1. - Scan from the second element onwards.
- If the current value differs from
nums[write - 1]— the last value kept — write it atnums[write]and advance. - Return the first
writeelements.
Why it works
Sortedness turns "have I seen this value?" into a single comparison against the previous kept value: any earlier occurrence of the same value must be adjacent to it, so nothing further back needs checking. That is why no hash set is needed and the extra space stays constant. The write pointer trails the read pointer, so values are never overwritten before being examined.
Complexity
- Time —
O(n) - Space —
O(1)
Pitfalls
- Compare against the last kept element (
nums[write - 1]), not the previous element in the original array. - Seed
write = 1, since the first element is always kept. - This relies on the input being sorted; on unsorted data it silently keeps duplicates.
- A single-element array must be returned unchanged, which the seeding covers.
Reference solution
Python
from typing import List
def removeDuplicates(nums: List[int]) -> List[int]:
out = []
for x in nums:
if not out or out[-1] != x:
out.append(x)
return outJava
public static int[] removeDuplicates(int[] nums) {
int write = 1;
for (int i = 1; i < nums.length; i++) {
if (nums[i] != nums[write - 1]) {
nums[write] = nums[i];
write++;
}
}
return Arrays.copyOf(nums, write);
}C++
vector<int> removeDuplicates(vector<int>& nums) {
int write = 1;
for (int i = 1; i < (int) nums.size(); i++) {
if (nums[i] != nums[write - 1]) {
nums[write] = nums[i];
write++;
}
}
return vector<int>(nums.begin(), nums.begin() + write);
}JavaScript
var removeDuplicates = function(nums) {
const out = [];
for (const x of nums) {
if (out.length === 0 || out[out.length - 1] !== x) out.push(x);
}
return out;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays · Two Pointers Technique