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.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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:
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:
[4, 2, 3, 3, 5, 7].[3, 5, 7], which has distinct elements.Therefore, the answer is 2.
Example 2:
Input: nums = [4,5,6,4,4]
Output: 2
Explanation:
[4, 4].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.
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 100Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,2,3,4,2,3,3,5,7]
[4,5,6,4,4]
[6,7,8,9]
minimum-increment-to-make-array-unique)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3396: Minimum Number of Operations to Make Elements in Array Distinct
class Solution {
public int minimumOperations(int[] nums) {
Set<Integer> s = new HashSet<>();
for (int i = nums.length - 1; i >= 0; --i) {
if (!s.add(nums[i])) {
return i / 3 + 1;
}
}
return 0;
}
}
// Accepted solution for LeetCode #3396: Minimum Number of Operations to Make Elements in Array Distinct
func minimumOperations(nums []int) int {
s := map[int]bool{}
for i := len(nums) - 1; i >= 0; i-- {
if s[nums[i]] {
return i/3 + 1
}
s[nums[i]] = true
}
return 0
}
# Accepted solution for LeetCode #3396: Minimum Number of Operations to Make Elements in Array Distinct
class Solution:
def minimumOperations(self, nums: List[int]) -> int:
s = set()
for i in range(len(nums) - 1, -1, -1):
if nums[i] in s:
return i // 3 + 1
s.add(nums[i])
return 0
// Accepted solution for LeetCode #3396: Minimum Number of Operations to Make Elements in Array Distinct
use std::collections::HashSet;
impl Solution {
pub fn minimum_operations(nums: Vec<i32>) -> i32 {
let mut s = HashSet::new();
for i in (0..nums.len()).rev() {
if !s.insert(nums[i]) {
return (i / 3) as i32 + 1;
}
}
0
}
}
// Accepted solution for LeetCode #3396: Minimum Number of Operations to Make Elements in Array Distinct
function minimumOperations(nums: number[]): number {
const s = new Set<number>();
for (let i = nums.length - 1; ~i; --i) {
if (s.has(nums[i])) {
return Math.ceil((i + 1) / 3);
}
s.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.