Back to DSA

Remove Duplicates from Sorted Array

easy
Acceptance: 58%
ArraysTwo Pointers

You are given a sorted integer array arranged in non-decreasing order. Your task is to eliminate repeated values so that every element in the array is unique. Perform the operation in-place, preserving the original relative ordering of the remaining elements. Return the count of distinct values, and ensure those values occupy the first positions of the array.

Examples

Example 1:
Input:arr = [2,2,5]
Output:2, arr = [2,5,_]
Explanation: There are 2 distinct values. After processing, positions 0 and 1 hold 2 and 5.
Example 2:
Input:arr = [1,1,3,3,3,5,5,7,7,9]
Output:5, arr = [1,3,5,7,9,_,_,_,_,_]
Explanation: Five unique elements remain: 1, 3, 5, 7, and 9, placed at the front of the array.

Hints

00:00
1234567