Skip to content

2206. Divide Array Into Equal Pairs #1444

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to determine if an integer array can be divided into pairs such that each pair consists of equal elements. The array has a length of 2n, meaning we need exactly n pairs. Each element must be part of exactly one pair, and all elements must be used.

Approach

The key insight here is that for each element to be paired successfully, each element must appear an even number of times. This is because each element needs exactly one other element of the same value to form a pair. If any element appears an odd number of times, it is impossible to form the required pairs, and we should return false. Conversely, if every element's count is even, we can form all the required pairs and return true.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kovatz
Comment options

kovatz Mar 17, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Mar 17, 2025
Maintainer Author

Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants