/80. Remove Duplicates from Sorted Array II

80. Remove Duplicates from Sorted Array II

Medium
Arrays64.4% acceptance

Given a sorted integer array arr in non-decreasing order, modify arr in-place so that each unique value appears at most twice. The relative order of elements must be preserved. Return the integer k, representing the number of elements in the modified array. The first k elements of arr should contain the result. Do not use extra space beyond O(1).

Example 1

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

Output: 6

Explanation: After removing extra duplicates, arr becomes [2,2,3,3,4,4].

Example 2

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

Output: 5

Explanation: After removing extra duplicates, arr becomes [5,5,6,6,7].

Example 3

Input: [8,8,8,8,8]

Output: 2

Explanation: After removing extra duplicates, arr becomes [8,8].

Constraints

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

Case 1

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

Expected: 7

Case 2

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

Expected: 8

Case 3

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

Expected: 7

Case 4

Input: [10,10,10,10,10,10]

Expected: 2

Case 5

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

Expected: 5