Minimum Number of Operations to Make Elements in Array Distinct

You are given an integer array nums. You need to ensure that the elements in the array are distinct. To achieve this, you can perform the following operation any number of times:

  • Remove 3 elements from the beginning of the array. If the array has fewer than 3 elements, remove all remaining elements.

Note that an empty array is considered to have distinct elements. Return the minimum number of operations needed to make the elements in the array distinct.

Example 1
Inputnums = [1,2,3,4,2,3,3,5,7]
Output2
After removing the first 3 elements, then the next 3 elements, the remaining array is [3, 5, 7], which has distinct elements, so the answer is 2.
Example 2
Inputnums = [4,5,6,4,4]
Output2
After removing the first 3 elements, the remaining array is [4, 4], and one more operation removes all remaining elements to make the array empty, so the answer is 2.

Constraints

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate