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 a 0-indexed integer array nums. In one operation, you may do the following:
nums that are equal.nums, forming a pair.The operation is done on nums as many times as possible.
Return a 0-indexed integer array answer of size 2 where answer[0] is the number of pairs that are formed and answer[1] is the number of leftover integers in nums after doing the operation as many times as possible.
Example 1:
Input: nums = [1,3,2,1,3,2,2] Output: [3,1] Explanation: Form a pair with nums[0] and nums[3] and remove them from nums. Now, nums = [3,2,3,2,2]. Form a pair with nums[0] and nums[2] and remove them from nums. Now, nums = [2,2,2]. Form a pair with nums[0] and nums[1] and remove them from nums. Now, nums = [2]. No more pairs can be formed. A total of 3 pairs have been formed, and there is 1 number leftover in nums.
Example 2:
Input: nums = [1,1] Output: [1,0] Explanation: Form a pair with nums[0] and nums[1] and remove them from nums. Now, nums = []. No more pairs can be formed. A total of 1 pair has been formed, and there are 0 numbers leftover in nums.
Example 3:
Input: nums = [0] Output: [0,1] Explanation: No pairs can be formed, and there is 1 number leftover in nums.
Constraints:
1 <= nums.length <= 1000 <= nums[i] <= 100Problem summary: You are given a 0-indexed integer array nums. In one operation, you may do the following: Choose two integers in nums that are equal. Remove both integers from nums, forming a pair. The operation is done on nums as many times as possible. Return a 0-indexed integer array answer of size 2 where answer[0] is the number of pairs that are formed and answer[1] is the number of leftover integers in nums after doing the operation as many times as possible.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,3,2,1,3,2,2]
[1,1]
[0]
sort-characters-by-frequency)top-k-frequent-words)sort-array-by-increasing-frequency)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2341: Maximum Number of Pairs in Array
class Solution {
public int[] numberOfPairs(int[] nums) {
int[] cnt = new int[101];
for (int x : nums) {
++cnt[x];
}
int s = 0;
for (int v : cnt) {
s += v / 2;
}
return new int[] {s, nums.length - s * 2};
}
}
// Accepted solution for LeetCode #2341: Maximum Number of Pairs in Array
func numberOfPairs(nums []int) []int {
cnt := [101]int{}
for _, x := range nums {
cnt[x]++
}
s := 0
for _, v := range cnt {
s += v / 2
}
return []int{s, len(nums) - s*2}
}
# Accepted solution for LeetCode #2341: Maximum Number of Pairs in Array
class Solution:
def numberOfPairs(self, nums: List[int]) -> List[int]:
cnt = Counter(nums)
s = sum(v // 2 for v in cnt.values())
return [s, len(nums) - s * 2]
// Accepted solution for LeetCode #2341: Maximum Number of Pairs in Array
impl Solution {
pub fn number_of_pairs(nums: Vec<i32>) -> Vec<i32> {
let n = nums.len();
let mut count = [0; 101];
for &v in nums.iter() {
count[v as usize] += 1;
}
let mut sum = 0;
for v in count.iter() {
sum += v >> 1;
}
vec![sum as i32, (n - sum * 2) as i32]
}
}
// Accepted solution for LeetCode #2341: Maximum Number of Pairs in Array
function numberOfPairs(nums: number[]): number[] {
const n = nums.length;
const count = new Array(101).fill(0);
for (const num of nums) {
count[num]++;
}
const sum = count.reduce((r, v) => r + (v >> 1), 0);
return [sum, n - sum * 2];
}
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.