215. Remove Duplicates from Sorted Array

Easy
Slow and Fast Pointers
Array

Problem

Given a sorted integer array nums, return an array containing each distinct value exactly once while preserving sorted order.

Examples

Example 1

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

Example 2

Input: [0,0,1,1,1,2,2,3,3,4]
Output: [0,1,2,3,4]

Example 3

Input: [1]
Output: [1]
Constraints

1 <= nums.length <= 100000

nums is sorted in non-decreasing order.

Hints

?? Keep one pointer at the last unique element.

?? Advance another pointer through the array.

Expected Complexity
Time: O(n)
Space: O(1) auxiliary
Follow-up

Can you perform the compaction in-place?

Practice Notes

Attempts: 0

Time spent: 0min

Hints used: 0

00:00
Loading...
Case 1
Case 2
Case 3
Input
[1,1,2]
Expected
[1,2]