Divide Array Into Equal Pairs — LeetCode 2206 Python Solution
EasyBit ManipulationArrayHash TableCounting
- Problem
- #2206
- Pattern
- Bit Manipulation
- Reading time
- 2 min
- Source
- leetcode.com
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
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(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
LeetCode 1255Maximum Score Words Formed by LettersHardLeetCode 1684Count the Number of Consistent StringsEasyLeetCode 1994The Number of Good SubsetsHardLeetCode 2275Largest Combination With Bitwise AND Greater Than ZeroMediumLeetCode 2506Count Pairs Of Similar StringsEasyLeetCode 169Majority ElementEasy
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.