Divide Array Into Equal Pairs

The goal is to find if there are enough pairs of each number in the array.

First, create a dictionary to count the number of occurrences of each number in the array. Iterate through the array and for each number, increment its count in the dictionary.

Then, go through the dictionary and check if the count of each number is even. If any number has an odd count, it means there aren’t enough pairs for that number, so you should return False.

If you’ve checked every number and they all have even counts, return True, since there are enough pairs for each number.

Here’s the Python code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution:
    def divideArray(self, nums: List[int]) -> bool:
        counts = {}
        for num in nums:
            if num in counts:
                counts[num] += 1
            else:
                counts[num] = 1

        for count in counts.values():
            if count % 2 != 0:
                return False
        return True

In the code, we use a dictionary to count the occurrences of each number in the array. We then check if all the counts are even. If they are, we return True, otherwise, we return False.