Problem Description
Given an integer array nums of length 2*n, determine if it is possible to split the array into n pairs such that the two numbers in each pair are equal.
Key Insights
- The array's length is even, ensuring that pairs can be formed if the conditions are met.
- For each distinct element in nums, its frequency must be even.
- Counting the frequency of elements is an efficient way to check if every element can form pairs.
- Alternatively, sorting the array and checking adjacent pairs is a viable solution.
Space and Time Complexity
Time Complexity: O(n), where n is the number of pairs (or O(2n) elements)
Space Complexity: O(n) in the worst case when all elements are unique
Solution
We can solve this problem by using a hash table (or dictionary) to count the frequency of each number in the array. Once the frequency count is computed, iterate over the hash table and check if every count is even. If any count is odd, then it is impossible to split nums into pairs such that each pair contains equal numbers. This approach efficiently determines the solution in one pass for counting and another pass over the unique keys.