Divide Array Into Equal Pairs — LeetCode 2206 Python Solution

EasyBit ManipulationArrayHash TableCounting
Problem
#2206
Reading time
2 min

The problem

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.

Example

Input
nums = [3,2,3,2,2,2]
Output
true
Explanation
There are 6 elements in nums, so they should be divided into 6 / 2 = 3 pairs.

Python solution

Python
class Solution:
    def divideArray(self, nums: List[int]) -> bool:
        cnt = Counter(nums)
        return all(v % 2 == 0 for v in cnt.values())

Complexity

MeasureComplexity
TimeO(n)
SpaceO(n) auxiliary

Pattern: Bit Manipulation

Use XOR, masks and the low-bit trick to replace whole data structures with an integer. LeetCode 2206. Divide Array Into Equal Pairs is filed here because LeetCode tags it Bit Manipulation, which is the vocabulary this hub collects.

The bit manipulation guide has the Python template for the pattern and the 194 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2206. Divide Array Into Equal Pairs?
LeetCode 2206. Divide Array Into Equal Pairs is rated Easy on LeetCode.
What is the time complexity of LeetCode 2206. Divide Array Into Equal Pairs?
The Python solution on this page runs in O(n).
What is the space complexity of LeetCode 2206. Divide Array Into Equal Pairs?
The Python solution on this page uses O(n) auxiliary space.
What topics does LeetCode 2206. Divide Array Into Equal Pairs cover?
LeetCode 2206. Divide Array Into Equal Pairs is tagged Bit Manipulation, Array, Hash Table and Counting on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview