Divide Array Into Equal Pairs (LeetCode 2206): Solution and Explanation

Easy
Array
Hash Table
Bit Manipulation
Counting

Given an array of 2n integers, decide whether it can be split into n pairs of equal values. [3,2,3,2,2,2] works as (2,2), (2,2), (3,3), while [1,2,3,4] does not.

Intuition

Pairs can be formed from any positions, so the order of the array is irrelevant. All that matters is how many copies of each value there are.

A value with an even count can be cut into pairs completely. A value with an odd count always leaves one copy over, and that copy cannot pair with a different value. So the answer is true exactly when every count is even.

Full counts are more than we need: only the parity of each count matters. Picture one switch per value. Each occurrence flips its switch, so a switch that is on means one copy is waiting for a partner, and the next copy turns it off and completes a pair. If a running total of switches that are on ends at 0, every value was paired.

Approach: Parity switch per value

  1. Create odd, a boolean array of size 501 (values go up to 500), and set unpaired = 0.
  2. For each x in nums, flip odd[x].
  3. If the switch is now on, increment unpaired; if it turned off, a pair was completed, so decrement it.
  4. Return unpaired == 0.

Step-by-step example

nums = [3,2,3,2,2,2]
  1. x = 3: odd[3] turns on, unpaired = 1.
  2. x = 2: odd[2] turns on, unpaired = 2.
  3. x = 3: odd[3] turns off, a (3, 3) pair is done, unpaired = 1.
  4. x = 2: odd[2] turns off, unpaired = 0.
  5. x = 2: odd[2] on again, unpaired = 1. x = 2: off, unpaired = 0.
  6. All switches are off, so return true.

Code: Python, Java, C++

1class Solution:
2    def divideArray(self, nums: List[int]) -> bool:
3        odd = [False] * 501
4        unpaired = 0
5        for x in nums:
6            odd[x] = not odd[x]
7            unpaired += 1 if odd[x] else -1
8        return unpaired == 0
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Parity switch per value O(n)
Count then check O(n)
Hash set toggle O(n)
Sort and compare neighbours O(n log n)
Time

O(n)

Each of the 2n elements flips one switch in constant time.

Space

O(1)

The switch array has a fixed size of 501 regardless of the input length.

Other approaches

Count then check (time O(n), space O(V))

Count every value in a hash map or array and return whether all counts are even. Equivalent, slightly more memory per value.

Sort and compare neighbours (time O(n log n), space O(1))

Sort the array and check that nums[0] == nums[1], nums[2] == nums[3], and so on. No extra structure, but the sort costs more.

Hash set toggle (time O(n), space O(V))

Add a value to a set the first time it is seen and remove it the next time. The set is empty at the end exactly when all counts are even.

Common mistakes

  • XORing the whole array and checking for 0. Different values can cancel: [1, 2, 4, 7] XORs to 0 although no value repeats. The parity has to be tracked per value, not per bit.
  • Checking only that the length is even. The length is always even here; the counts are what matter.
  • Sizing the array as 500 and then indexing nums[i] = 500, which is one past the end.

Frequently asked questions

Why is an even count for every value enough?

Copies of the same value can be paired with each other in any order, so a value with 2k copies forms k pairs. Different values never need to interact.

Why not XOR all elements together?

XOR tracks parity per bit, not per value, so different values can cancel each other. [1, 2, 4, 7] XORs to 0 and still has no pair. The per-value switch array avoids that.

Is the unpaired counter necessary?

No. You can scan the switch array at the end instead. The counter just gives the answer without a second pass.