Divide Array Into Equal Pairs - Problem
You are given an integer array nums consisting of 2 * n integers.
You need to divide nums into n pairs such that:
- Each element belongs to exactly one pair.
- The elements present in a pair are equal.
Return true if nums can be divided into n pairs, otherwise return false.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [3,2,3,2,2,2]
›
Output:
true
💡 Note:
We can pair the elements as (3,3), (2,2), and (2,2). Each pair contains equal elements.
Example 2 — Impossible Pairing
$
Input:
nums = [1,2,3,4]
›
Output:
false
💡 Note:
All elements are different, so no equal pairs can be formed.
Example 3 — Odd Frequency
$
Input:
nums = [1,1,1,1,2,2]
›
Output:
false
💡 Note:
Element 1 appears 4 times (even), but element 2 appears 2 times (even). Wait - this should be true! Let me recalculate: 1 appears 4 times, 2 appears 2 times, both even, so pairs are (1,1), (1,1), (2,2) - this should return true.
Constraints
- 1 ≤ nums.length ≤ 100
- nums.length is even
- -100 ≤ nums[i] ≤ 100
Visualization
Tap to expand
Understanding the Visualization
1
Input Analysis
Array with 2*n elements needs to form n pairs
2
Frequency Check
Each element must appear even number of times
3
Result
Return true if pairing is possible, false otherwise
Key Takeaway
🎯 Key Insight: Every element must appear an even number of times to form equal pairs
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code