2206. Divide Array Into Equal Pairs - Explanation

Problem Link



Prerequisites

Before attempting this problem, you should be comfortable with:

  • Hash Maps - Counting element frequencies efficiently in O(1) per operation
  • Hash Sets - Tracking elements with odd occurrences using add/remove toggle logic
  • Sorting - Grouping identical elements together for run-length analysis

1. Sorting

Intuition

For an array to be divisible into pairs of equal elements, every distinct value must appear an even number of times. By sorting the array, identical elements become adjacent. We can then scan through and count consecutive runs of equal values. If any run has an odd length, we cannot form valid pairs.

Algorithm

  1. Sort the array.
  2. Iterate through the sorted array, tracking runs of consecutive equal elements.
  3. For each run, check if its length is odd.
  4. If any run has odd length, return false.
  5. If all runs have even length, return true.
class Solution:
    def divideArray(self, nums: List[int]) -> bool:
        N = len(nums)
        nums.sort()

        i = 0
        while i < N:
            j = i
            while j < N and nums[i] == nums[j]:
                j += 1

            if (j - i) % 2 != 0:
                return False

            i = j

        return True

Time & Space Complexity

  • Time complexity: O(nlogn)O(n \log n)
  • Space complexity: O(1)O(1) or O(n)O(n) depending on the sorting algorithm.

2. Hash Map

Intuition

We can count the frequency of each element using a hash map. After counting, we check if every element appears an even number of times. If any element has an odd count, it cannot be fully paired, so we return false.

Algorithm

  1. Create a hash map to store the count of each element.
  2. Iterate through the array and increment the count for each element.
  3. Iterate through all counts in the hash map.
  4. If any count is odd, return false.
  5. Otherwise, return true.
class Solution:
    def divideArray(self, nums: List[int]) -> bool:
        count = {}
        for num in nums:
            if num not in count:
                count[num] = 0
            count[num] += 1

        for cnt in count.values():
            if cnt % 2 == 1:
                return False

        return True

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(n)O(n)

3. Hash Set

Intuition

Instead of counting all frequencies, we can use a set to track elements with odd occurrences. When we see an element, if it is already in the set (meaning we have seen it an odd number of times), we remove it. If it is not in the set, we add it. At the end, if the set is empty, all elements appeared an even number of times.

Algorithm

  1. Create an empty hash set.
  2. Iterate through each element in the array.
  3. If the element is in the set, remove it (completing a pair).
  4. Otherwise, add it to the set (unpaired element).
  5. After processing all elements, return true if the set is empty, false otherwise.
class Solution:
    def divideArray(self, nums: List[int]) -> bool:
        odd_set = set()

        for num in nums:
            if num not in odd_set:
                odd_set.add(num)
            else:
                odd_set.remove(num)

        return not len(odd_set)

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(n)O(n)

Common Pitfalls

Checking for Pairs Instead of Even Counts

A common mistake is trying to actually form pairs and check if adjacent elements match after sorting. The problem only requires checking if each element appears an even number of times, not that the pairs are adjacent.

# Wrong: Checking adjacent pairs after sorting
nums.sort()
for i in range(0, len(nums), 2):
    if nums[i] != nums[i + 1]:  # IndexError if odd length
        return False

# Correct: Just check if each count is even
for cnt in count.values():
    if cnt % 2 == 1:
        return False

Forgetting the Array Length is Always Even

The problem guarantees that nums has 2n elements. Some solutions add unnecessary checks for odd-length arrays or forget that the total count of elements is always even, which simplifies the problem to just checking individual element frequencies.