Skip to main content

Divide Array Into Equal Pairs - Solution & Explanation

EasyArrayHash TableBit ManipulationCounting14 min readAsked at: Amazon, Microsoft, Meta +2
Practice this problem

Problem Statement

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.
  • The elements present in a pair are equal.

Return true if nums can be divided into n pairs, otherwise return false.

 

Example 1:

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.
If nums is divided into the pairs (2, 2), (3, 3), and (2, 2), it will satisfy all the conditions.

Example 2:

Input: nums = [1,2,3,4]
Output: false
Explanation: 
There is no way to divide nums into 4 / 2 = 2 pairs such that the pairs satisfy every condition.

 

Constraints:

  • nums.length == 2 * n
  • 1 <= n <= 500
  • 1 <= nums[i] <= 500

Approach Overview

Problem Overview: You are given an integer array with an even number of elements. The task is to check whether the array can be divided into pairs such that both elements in every pair are equal. In practice, this means every value in the array must appear an even number of times.

Approach 1: Frequency Count Approach (O(n) time, O(n) space)

This approach counts how many times each number appears using a hash-based frequency table. Iterate through the array once and increment the count for each value. After building the frequency map, iterate over the stored counts and verify that every frequency is even. If any value has an odd frequency, forming equal pairs becomes impossible.

The key insight: each pair consumes exactly two identical elements. A value appearing 2k times can form k valid pairs, but a value appearing 2k + 1 times leaves one element unmatched. Hash lookups and updates are constant time, so the overall complexity stays linear. This approach relies on a hash table and simple counting, making it the most direct and scalable solution for unsorted arrays.

Approach 2: Sorting Approach (O(n log n) time, O(1) or O(n) space)

Another strategy sorts the array first, grouping identical numbers together. After sorting, iterate through the array in steps of two. For each step, compare nums[i] with nums[i+1]. If the two values differ, the array cannot be partitioned into equal pairs.

Sorting ensures duplicates appear next to each other, which simplifies verification. This method uses a common array technique: sequential pair comparison after ordering elements. While the logic is straightforward, sorting introduces an O(n log n) time cost. Space complexity depends on the sorting algorithm—some languages use in-place sorting with O(1) extra space, while others allocate additional buffers.

Recommended for interviews: The frequency count approach is the expected solution. Interviewers typically want to see that you recognize the parity constraint—each number must appear an even number of times. Implementing it with a hash map shows familiarity with counting patterns and constant-time lookups. The sorting approach is still valid and demonstrates problem-solving flexibility, but the linear-time hash counting solution better highlights algorithmic efficiency.

Approach 1: Frequency Count Approach

This approach involves counting the frequency of each number in the array. If all frequencies are even, then it is possible to pair the numbers; otherwise, it is not.

The solution creates a frequency count array `count` where each index corresponds to a number in `nums`. As it iterates through `nums`, it increments the frequency for each number. Finally, it checks all frequencies to ensure they're even, which is necessary for perfect pairing. If any frequency is odd, it returns `false`; otherwise, `true`.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is numsSize. Space Complexity: O(1), since the frequency array size is fixed and independent of input size.

Try this approach in the editor →

Approach 2: Sorting Approach

This approach involves sorting the array and then checking adjacent elements in pairs. If all pairs are identical, then the array can be divided into equal pairs.

The C solution sorts the array using `qsort` and then compares every adjacent pair for equality. If any pair is not equal, `false` is returned.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n log n) due to sorting. Space Complexity: O(1) if the sort is in-place.

Try this approach in the editor →

Approach 3: Counting

According to the problem description, as long as each element in the array appears an even number of times, the array can be divided into n pairs.

Therefore, we can use a hash table or an array cnt to record the number of occurrences of each element, then traverse cnt. If any element appears an odd number of times, return false; otherwise, return true.

The time complexity is O(n), and the space complexity is O(n). Here, n is the length of the array nums.

Code

Python

Java

C++

Go

Rust

TypeScript

JavaScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Frequency Count Approach

Time Complexity: O(n), where n is numsSize. Space Complexity: O(1), since the frequency array size is fixed and independent of input size.

Sorting Approach

Time Complexity: O(n log n) due to sorting. Space Complexity: O(1) if the sort is in-place.

Counting—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Frequency Count (Hash Map)O(n)O(n)General case when the array is unsorted and you want the optimal linear-time solution
Sorting + Pair CheckO(n log n)O(1) to O(n)Useful when sorting is already required elsewhere or when avoiding extra hash structures

Video Solution

Divide Array Into Equal Pairs - Leetcode 2206 - Python • NeetCodeIO • 6,144 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Divide Array Into Equal Pairs easy or hard?
Divide Array Into Equal Pairs is categorized as an Easy problem on LeetCode with a high acceptance rate. The core idea is recognizing that every number must appear an even number of times, which can be verified with a simple frequency count.
Divide Array Into Equal Pairs Python/Java solution
In Python, use a dictionary or collections.Counter to track frequencies and check if each count is even. In Java, a HashMap<Integer, Integer> performs the same role. Both implementations follow the same O(n) counting strategy.
How to solve Divide Array Into Equal Pairs in O(n)?
Traverse the array and store counts of each number in a hash map. After counting, check that every frequency is divisible by two. If any count is odd, return false; otherwise return true. Hash table operations keep the overall complexity at O(n).
What is the best approach for Divide Array Into Equal Pairs?
The best approach uses a hash map to count the frequency of each number. Iterate through the array, record how many times each value appears, and verify that every frequency is even. This method runs in O(n) time with O(n) extra space and works for any unsorted array.
Is Divide Array Into Equal Pairs asked at Google/Amazon/Meta?
Problems involving frequency counting and pair validation commonly appear in interviews at companies like Amazon, Google, and Meta. This problem specifically tests hash table usage, counting logic, and reasoning about element parity in arrays.
What data structure is used in Divide Array Into Equal Pairs?
A hash table (or dictionary) is typically used to store element frequencies. It allows constant-time insertion and lookup while counting occurrences, which enables the optimal O(n) solution.
What is the time complexity of Divide Array Into Equal Pairs?
The optimal solution runs in O(n) time using a frequency count with a hash table. Each element is processed once while updating counts. A sorting-based alternative requires O(n log n) time because of the sort operation.

Ready to solve this problem?

Practice Divide Array Into Equal Pairs with our built-in code editor and test cases.

Practice on FleetCode