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,2,2]
›
Output:
true
💡 Note:
Element 1 appears 2 times (even) and element 2 appears 2 times (even). We can form pairs (1,1) and (2,2).
Constraints
- 1 ≤ nums.length ≤ 100
- nums.length is even
- -100 ≤ nums[i] ≤ 100
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code