I use a frequency counter to count the occurrences of each number in nums. If every number appears an even number of times, then nums can be divided into valid pairs. Else it's not possible to divide the array into required pairs.
class Solution:
def divideArray(self, nums: List[int]) -> bool:
count = Counter(nums)
return all(v % 2 == 0 for v in count.values())
- Time: O(n)
- Space: O(n)
