/26. Remove Duplicates from Sorted Array

26. Remove Duplicates from Sorted Array

Easy
Arrays62.4% acceptance

Given a sorted integer array arr in non-decreasing order, modify arr in-place so that each unique element appears only once, maintaining their original order. Return the number of unique elements, n. The first n elements of arr should contain the unique values in sorted order. Elements beyond index n-1 can be ignored.

Example 1

Input: [2,2,3,3,4,5,5]

Output: 4

Explanation: After removing duplicates, arr = [2,3,4,5,...], n = 4.

Example 2

Input: [-5,-5,-5,-5]

Output: 1

Explanation: After removing duplicates, arr = [-5,...], n = 1.

Example 3

Input: [7,8,9,10]

Output: 4

Explanation: No duplicates, arr = [7,8,9,10], n = 4.

Constraints

  • 1 <= len(arr) <= 30000
  • -100 <= arr[i] <= 100
  • arr is sorted in non-decreasing order
Python (current runtime)

Case 1

Input: [1,1,1,2,2,3]

Expected: 3

Case 2

Input: [-2,-1,-1,0,0,0,1]

Expected: 4

Case 3

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

Expected: 5

Case 4

Input: [100]

Expected: 1

Case 5

Input: [-100,-100,-99,-98,-98,-97]

Expected: 4