Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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:
1 <= nums.length <= 1051 <= nums[i] <= 105Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[3,8,3,6,5,8]
[2,2]
[4,3,5,1,2]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3779: Minimum Number of Operations to Have Distinct Elements
class Solution {
public int minOperations(int[] nums) {
Set<Integer> st = new HashSet<>();
for (int i = nums.length - 1; i >= 0; --i) {
if (!st.add(nums[i])) {
return i / 3 + 1;
}
}
return 0;
}
}
// Accepted solution for LeetCode #3779: Minimum Number of Operations to Have Distinct Elements
func minOperations(nums []int) int {
st := make(map[int]struct{})
for i := len(nums) - 1; i >= 0; i-- {
if _, ok := st[nums[i]]; ok {
return i/3 + 1
}
st[nums[i]] = struct{}{}
}
return 0
}
# Accepted solution for LeetCode #3779: Minimum Number of Operations to Have Distinct Elements
class Solution:
def minOperations(self, nums: List[int]) -> int:
st = set()
for i in range(len(nums) - 1, -1, -1):
if nums[i] in st:
return i // 3 + 1
st.add(nums[i])
return 0
// Accepted solution for LeetCode #3779: Minimum Number of Operations to Have Distinct Elements
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3779: Minimum Number of Operations to Have Distinct Elements
// class Solution {
// public int minOperations(int[] nums) {
// Set<Integer> st = new HashSet<>();
// for (int i = nums.length - 1; i >= 0; --i) {
// if (!st.add(nums[i])) {
// return i / 3 + 1;
// }
// }
// return 0;
// }
// }
// Accepted solution for LeetCode #3779: Minimum Number of Operations to Have Distinct Elements
function minOperations(nums: number[]): number {
const st = new Set<number>();
for (let i = nums.length - 1; i >= 0; i--) {
if (st.has(nums[i])) {
return Math.floor(i / 3) + 1;
}
st.add(nums[i]);
}
return 0;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.