Remove Duplicates From Sorted Array
01 · Problem
You are given an integer array nums sorted in non-decreasing order. Remove the repeated values so that each distinct value appears exactly once, keeping the values in their original (sorted) order.
Return the array of unique values. In an in-place implementation this is the first k elements of nums after deduplication, where k is the number of distinct values; anything left beyond position k is ignored and must not be returned.
02 · Examples
nums = [1,1,2]
[1,2]
The second 1 is dropped, leaving the distinct values 1 and 2.
nums = [0,0,1,1,1,2,2,3,3,4]
[0,1,2,3,4]
Each run of equal values collapses to a single copy.
nums = [-3,-3,-3]
[-3]
Every element is the same value, so only one copy remains.
03 · Constraints
- 011 <= nums.length <= 3 * 104
- 02-100 <= nums[i] <= 100
- 03nums is sorted in non-decreasing order
04 · Optimal complexity
- Time
- O(n)
- Space
- O(1)
Practice it alone or rehearse it as an interview.
Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.