Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package g3701_3800.s3779_minimum_number_of_operations_to_have_distinct_elements;

// #Medium #Array #Hash_Table #Senior #Biweekly_Contest_172
// #2026_05_06_Time_4_ms_(99.25%)_Space_132.03_MB_(83.46%)

public class Solution {
public int minOperations(int[] nums) {
boolean[] seen = new boolean[100001];
int i = nums.length - 1;
while (i >= 0) {
if (seen[nums[i]]) {
break;
}
seen[nums[i]] = true;
i--;
}
return (i + 3) / 3;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
3779\. Minimum Number of Operations to Have Distinct Elements

Medium

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:**

**Input:** nums = [3,8,3,6,5,8]

**Output:** 1

**Explanation:**

In the first operation, we remove the first three elements. The remaining elements `[6, 5, 8]` are all distinct, so we stop. Only one operation is needed.

**Example 2:**

**Input:** nums = [2,2]

**Output:** 1

**Explanation:**

After one operation, the array becomes empty, which meets the stopping condition.

**Example 3:**

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

**Output:** 0

**Explanation:**

All elements in the array are distinct, therefore no operations are needed.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g3701_3800.s3779_minimum_number_of_operations_to_have_distinct_elements;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void minOperations() {
assertThat(new Solution().minOperations(new int[] {3, 8, 3, 6, 5, 8}), equalTo(1));
}

@Test
void minOperations2() {
assertThat(new Solution().minOperations(new int[] {2, 2}), equalTo(1));
}

@Test
void minOperations3() {
assertThat(new Solution().minOperations(new int[] {4, 3, 5, 1, 2}), equalTo(0));
}
}
Loading