LeetCode 2206: Divide Array Into Equal Pairs (Frequency Parity Check)
LeetCode 2206ArrayHash MapToday we solve LeetCode 2206 - Divide Array Into Equal Pairs.
Source: https://leetcode.com/problems/divide-array-into-equal-pairs/
English
Problem Summary
Given an integer array nums of even length, determine whether you can split all elements into pairs such that each pair contains two equal numbers.
Key Insight
If every value appears an even number of times, we can always form equal pairs. So the task becomes a parity check on frequencies.
Brute Force and Limitations
Sort then scan in steps of 2 and verify each adjacent pair matches. It works in O(n log n), but sorting is unnecessary for this yes/no check.
Optimal Algorithm Steps
1) Count frequency of each value.
2) Traverse all frequencies.
3) If any count is odd, return false.
4) Otherwise return true.
Complexity Analysis
Time: O(n).
Space: O(n).
Common Pitfalls
- Forgetting that nums.length is even does not guarantee each value count is even.
- Using XOR tricks that fail when numbers appear more than twice.
- Returning true before validating all counts.
Reference Implementations (Java / Go / C++ / Python / JavaScript)
class Solution {
public boolean divideArray(int[] nums) {
Map<Integer, Integer> cnt = new HashMap<>();
for (int x : nums) {
cnt.put(x, cnt.getOrDefault(x, 0) + 1);
}
for (int c : cnt.values()) {
if ((c & 1) == 1) {
return false;
}
}
return true;
}
}func divideArray(nums []int) bool {
cnt := map[int]int{}
for _, x := range nums {
cnt[x]++
}
for _, c := range cnt {
if c%2 == 1 {
return false
}
}
return true
}class Solution {
public:
bool divideArray(vector<int>& nums) {
unordered_map<int, int> cnt;
for (int x : nums) {
cnt[x]++;
}
for (auto& [_, c] : cnt) {
if (c % 2 != 0) {
return false;
}
}
return true;
}
};from collections import Counter
class Solution:
def divideArray(self, nums: list[int]) -> bool:
cnt = Counter(nums)
return all(c % 2 == 0 for c in cnt.values())/**
* @param {number[]} nums
* @return {boolean}
*/
var divideArray = function(nums) {
const cnt = new Map();
for (const x of nums) {
cnt.set(x, (cnt.get(x) || 0) + 1);
}
for (const c of cnt.values()) {
if (c % 2 !== 0) return false;
}
return true;
};中文
题目概述
给定一个长度为偶数的整数数组 nums,判断是否可以把所有元素分成若干对,并且每一对里的两个数字都相等。
核心思路
只要每个数字的出现次数都是偶数,就一定能两两配对成功。因此本题本质是做频次奇偶性检查。
暴力解法与不足
先排序再每隔两个位置检查是否相等,复杂度 O(n log n)。能做,但不是最优,因为我们只需要判断可行性。
最优算法步骤
1)统计每个数字出现次数。
2)遍历所有频次。
3)只要有奇数次,立即返回 false。
4)全部为偶数则返回 true。
复杂度分析
时间复杂度:O(n)。
空间复杂度:O(n)。
常见陷阱
- 误以为数组长度是偶数就一定可配对。
- 用 XOR 思路处理,会在某些出现次数大于 2 的情况下失效。
- 没检查完所有频次就提前返回 true。
多语言参考实现(Java / Go / C++ / Python / JavaScript)
class Solution {
public boolean divideArray(int[] nums) {
Map<Integer, Integer> cnt = new HashMap<>();
for (int x : nums) {
cnt.put(x, cnt.getOrDefault(x, 0) + 1);
}
for (int c : cnt.values()) {
if ((c & 1) == 1) {
return false;
}
}
return true;
}
}func divideArray(nums []int) bool {
cnt := map[int]int{}
for _, x := range nums {
cnt[x]++
}
for _, c := range cnt {
if c%2 == 1 {
return false
}
}
return true
}class Solution {
public:
bool divideArray(vector<int>& nums) {
unordered_map<int, int> cnt;
for (int x : nums) {
cnt[x]++;
}
for (auto& [_, c] : cnt) {
if (c % 2 != 0) {
return false;
}
}
return true;
}
};from collections import Counter
class Solution:
def divideArray(self, nums: list[int]) -> bool:
cnt = Counter(nums)
return all(c % 2 == 0 for c in cnt.values())/**
* @param {number[]} nums
* @return {boolean}
*/
var divideArray = function(nums) {
const cnt = new Map();
for (const x of nums) {
cnt.set(x, (cnt.get(x) || 0) + 1);
}
for (const c of cnt.values()) {
if (c % 2 !== 0) return false;
}
return true;
};
Comments