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.
Function Signature
def divide_array(nums: list[int]) -> bool:
Rules
-
Pairs are made from array positions, so a value that appears four times can form two pairs.
Constraints
-
len(nums) == 2 * n
-
1 <= n <= 500
-
1 <= nums[i] <= 500
Examples
Input: nums = [3,2,3,2,2,2]
Output: True
There are 6 elements, so 3 pairs are needed. The pairs (2, 2), (3, 3) and (2, 2) use every element once.
Input: nums = [1,2,3,4]
Output: False
There is no way to divide the array into pairs of equal elements. For example, 1 appears only once and has no partner.
Input: nums = [7,7,7,8,8,8]
Output: False
The length is even, but each value appears three times. After pairing two 7s and two 8s, one 7 and one 8 are left over, and they are not equal.