/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 and the relative order is preserved. Return the number of unique elements, n_unique. The first n_unique elements of arr must contain the unique values in sorted order. Elements beyond index n_unique - 1 can be ignored.

Example 1

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

Output: 4

Explanation: Unique values are [2,3,5,7], so n_unique = 4. arr[:4] should be [2,3,5,7].

Example 2

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

Output: 5

Explanation: Unique values are [-3,-2,-1,0,1], so n_unique = 5. arr[:5] should be [-3,-2,-1,0,1].

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,1,1]

Expected: 1

Case 2

Input: [4,5,6,7,8]

Expected: 5

Case 3

Input: [-10,-10,-10,-9,-8,-8,-7]

Expected: 4

Case 4

Input: [0,1,2,3,4,5,6,7,8,9]

Expected: 10

Case 5

Input: [-2,-2,-2,-2,-2,-2,-2,-2,-2,-2]

Expected: 1