Practice

Remove Duplicates from Sorted Array

Module 4 · Arrays & Dynamic Arrays

Problem

Given an integer array nums sorted in non-decreasing order, remove the duplicates in place so that each unique element appears once, keeping relative order. Return k, the number of unique elements. The first k slots of nums must hold the answer; what's past them doesn't matter.

Goal

Compact uniques into the prefix. Return length k — tests only read nums[0..k).

Examples

Example 1

Inputnums = [1, 1, 2]Outputk = 2, nums = [1, 2, _]

Example 2

Inputnums = [0,0,1,1,1,2,2,3,3,4]Outputk = 5, nums = [0,1,2,3,4,_,_,_,_,_]

Constraints

1 ≤ n ≤ 3·10⁴ · values in [−100, 100] · nums is sorted · O(1) auxiliary space

Attempt it first

Tip

You have every tool: this is the write-pointer template from the previous lesson, with one twist to find. Genuinely try before opening anything.