Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Remove duplicates from a sorted array

Remove duplicates in-place from a sorted array and return the count of unique elements.

Python practice10 minArrays – Fundamentals & PatternsBeginnerLast updated March 25, 2026

Problem statement

Given a sorted list nums, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. You should not allocate extra space for another list; modify the input list in-place with O(1) extra memory. Return the number of unique elements (an integer).

Task

Implement an in-place algorithm to remove duplicates from a non-decreasing sorted list and return the new length (number of unique elements).

Examples

Example with duplicates

Input

remove_duplicates([1, 1, 2])

Output

2

The unique elements are [1, 2], so the function returns 2.

Input format

A single sorted list of integers (non-decreasing order).

Output format

An integer representing the number of unique elements after in-place removal of duplicates.

Constraints

- 0 <= len(nums) <= 10^5 - nums is sorted in non-decreasing order - Must run in O(n) time and O(1) extra space

Samples

Sample 1

Input

remove_duplicates([0, 0, 1, 1, 1, 2, 2, 3, 3, 4])

Output

5

Unique elements are [0,1,2,3,4], so the returned length is 5.