Mid/SeniorArrayHash Table

Minimum Number of Operations to Have Distinct Elements

You are given an integer array nums.

In one operation, you remove the first three elements of the current array. If there are fewer than three elements remaining, all remaining elements are removed.

Repeat this operation until the array is empty or contains no duplicate values.

Return an integer denoting the number of operations required.

Example 1
Inputnums = [3,8,3,6,5,8]
Output1
In the first operation, the first three elements are removed, leaving [6, 5, 8], which are all distinct.
Example 2
Inputnums = [2,2]
Output1
After one operation, the array becomes empty, which meets the stopping condition.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^5

Asked at 1 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