Maximum Number of Pairs in Array
EasyUpdated: Aug 2, 2025
Practice on:
Problem
You are given a 0-indexed integer array nums. In one operation, you may do the following:
- Choose two integers in
numsthat are equal. - Remove both integers from
nums, forming a pair.
The operation is done on nums as many times as possible.
Return _a0-indexed integer array _answer of size2
whereanswer[0]is the number of pairs that are formed andanswer[1]is the number of leftover integers innums after doing the operation as many times as possible.
Examples
Example 1
Input: nums = [1,3,2,1,3,2,2]
Output: [3,1]
Explanation:
Form a pair with nums[0] and nums[3] and remove them from nums. Now, nums = [3,2,3,2,2].
Form a pair with nums[0] and nums[2] and remove them from nums. Now, nums = [2,2,2].
Form a pair with nums[0] and nums[1] and remove them from nums. Now, nums = [2].
No more pairs can be formed. A total of 3 pairs have been formed, and there is 1 number leftover in nums.
Example 2
Input: nums = [1,1]
Output: [1,0]
Explanation: Form a pair with nums[0] and nums[1] and remove them from nums. Now, nums = [].
No more pairs can be formed. A total of 1 pair has been formed, and there are 0 numbers leftover in nums.
Example 3
Input: nums = [0]
Output: [0,1]
Explanation: No pairs can be formed, and there is 1 number leftover in nums.
Constraints
1 <= nums.length <= 1000 <= nums[i] <= 100
Solution
Method 1 – Counting with Hash Map
Intuition
To maximize the number of pairs, count the frequency of each number. Each pair requires two equal numbers, so the number of pairs for each value is count // 2. The leftovers are the numbers that couldn't be paired.
Approach
- Count the frequency of each number in the array using a hash map.
- For each number, add
count // 2to the total pairs andcount % 2to the leftovers. - Return the total pairs and leftovers as the answer.
Code
C++
class Solution {
public:
vector<int> numberOfPairs(vector<int>& nums) {
unordered_map<int, int> cnt;
for (int x : nums) cnt[x]++;
int pairs = 0, left = 0;
for (auto& [k, v] : cnt) {
pairs += v / 2;
left += v % 2;
}
return {pairs, left};
}
};
Go
func numberOfPairs(nums []int) []int {
cnt := map[int]int{}
for _, x := range nums {
cnt[x]++
}
pairs, left := 0, 0
for _, v := range cnt {
pairs += v / 2
left += v % 2
}
return []int{pairs, left}
}
Java
import java.util.*;
class Solution {
public int[] numberOfPairs(int[] nums) {
Map<Integer, Integer> cnt = new HashMap<>();
for (int x : nums) cnt.put(x, cnt.getOrDefault(x, 0) + 1);
int pairs = 0, left = 0;
for (int v : cnt.values()) {
pairs += v / 2;
left += v % 2;
}
return new int[]{pairs, left};
}
}
Kotlin
class Solution {
fun numberOfPairs(nums: IntArray): IntArray {
val cnt = mutableMapOf<Int, Int>()
for (x in nums) cnt[x] = cnt.getOrDefault(x, 0) + 1
var pairs = 0
var left = 0
for (v in cnt.values) {
pairs += v / 2
left += v % 2
}
return intArrayOf(pairs, left)
}
}
Python
class Solution:
def numberOfPairs(self, nums: list[int]) -> list[int]:
from collections import Counter
cnt = Counter(nums)
pairs = left = 0
for v in cnt.values():
pairs += v // 2
left += v % 2
return [pairs, left]
Rust
use std::collections::HashMap;
impl Solution {
pub fn number_of_pairs(nums: Vec<i32>) -> Vec<i32> {
let mut cnt = HashMap::new();
for x in nums {
*cnt.entry(x).or_insert(0) += 1;
}
let mut pairs = 0;
let mut left = 0;
for &v in cnt.values() {
pairs += v / 2;
left += v % 2;
}
vec![pairs, left]
}
}
TypeScript
class Solution {
numberOfPairs(nums: number[]): number[] {
const cnt = new Map<number, number>();
for (const x of nums) cnt.set(x, (cnt.get(x) || 0) + 1);
let pairs = 0, left = 0;
for (const v of cnt.values()) {
pairs += Math.floor(v / 2);
left += v % 2;
}
return [pairs, left];
}
}
Complexity
- ⏰ Time complexity:
O(n), wherenis the length ofnums, since we count each number once. - 🧺 Space complexity:
O(n), for the hash map storing counts.