Skip to main content

Remove Duplicates from Sorted Array

Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same.

Problems52 / 152
Sign in

Remove Duplicates from Sorted Array

EASYArrayTwo Pointers

Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same.

Example 1

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

Example 2

Input:  nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5
Show hint

Use two pointers: one to iterate through the array, another to track the position where unique elements should be placed.

Draft saves automatically

Loading editor...

Submit also runs hidden tests
nums =
[1,1,2]
Expected
2
Run with your own input (JSON)