DDSA Solutions

2206. Divide Array Into Equal Pairs

Time: O(n)
Space: O(n)

Problem Overview

Divide array into pairs of equal values.

Advertisement

Intuition

Divide array into pairs of equal values. For each distinct value appearing c times, you can form c/2 pairs (integer division). Sum pair counts across all values — order of pairing does not matter.

Algorithm

  1. 1Build frequency map value -> count.
  2. 2Initialize pairs = 0.
  3. 3For each count c in map: pairs += c / 2 (integer division).
  4. 4Return pairs.

Example Walkthrough

Input: nums = [3,2,3,2,2,2]

  1. 1.Counts: 3 appears 2 -> 1 pair. 2 appears 4 -> 2 pairs. Total 3.

Output: 3

Common Pitfalls

  • Use integer division c/2, not combinations formula on whole array.
  • Odd leftover elements cannot form a pair — discard them.
  • Frequency map handles unsorted input.
2206.cs
C#
// Approach: Count occurrences; all counts must be even to form pairs.
// Time: O(n) Space: O(n)

public class Solution
{
    public bool DivideArray(int[] nums)
    {
        int[] count = new int[501];

        foreach (var num in nums)
            ++count[num];

        return count.All(c => c % 2 == 0);
    }
}
Advertisement
Was this solution helpful?

Related Problems