3396. 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

Input: nums = [1,2,3,4,2,3,3,5,7]
Output: 2

Explanation:

In the first operation, the first 3 elements are removed, resulting in the array [4, 2, 3, 3, 5, 7].
In the second operation, the next 3 elements are removed, resulting in the array [3, 5, 7], which has distinct elements.
Therefore, the answer is 2.

Example 2

Input: nums = [4,5,6,4,4]
Output: 2

Explanation:
In the first operation, the first 3 elements are removed, resulting in the array [4, 4].
In the second operation, all remaining elements are removed, resulting in an empty array.
Therefore, the answer is 2.

Example 3

Input: nums = [6,7,8,9]
Output: 0

Explanation:
The array already contains distinct elements. Therefore, the answer is 0.

Method 1

【O(n) time | O(n) space】
package Leetcode.Math;

import java.util.HashSet;
import java.util.Set;

/**
 * @author zhengxingxing
 * @date 2025/04/08
 */
public class MinimumNumberOfOperationsToMakeElementsInArrayDistinct {
    
    public static int minimumOperations(int[] nums) {
        // HashSet to store unique elements seen so far
        Set<Integer> seen = new HashSet<>();

        // Traverse array from end to start
        for (int i = nums.length - 1; i >= 0; i--) {
            // Try to add current element to set
            // seen.add() returns false if element already exists
            if (!seen.add(nums[i])) {
                // When duplicate found:
                // i/3 + 1 calculates ceiling of (i+1)/3
                // This gives minimum operations needed to remove elements up to index i
                return i / 3 + 1;
            }
        }
        // Return 0 if no duplicates found
        return 0;
    }

    public static void main(String[] args) {
        // Test case 1: Array with multiple duplicates
        int[] nums1 = {1,2,3,4,2,3,3,5,7};
        System.out.println("Test case 1 result: " + minimumOperations(nums1)); // Expected: 2

        // Test case 2: Array with duplicates near end
        int[] nums2 = {4,5,6,4,4};
        System.out.println("Test case 2 result: " + minimumOperations(nums2)); // Expected: 2

        // Test case 3: Array with no duplicates
        int[] nums3 = {6,7,8,9};
        System.out.println("Test case 3 result: " + minimumOperations(nums3)); // Expected: 0
    }
}




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • 1759. Count Number of Homogenous Substrings
  • 2038. Remove Colored Pieces if Both Neighbors are the Same Color
  • 845. Longest Mountain in Array
  • 1534. Count Good Triplets
  • 228. Summary Ranges