File: divide-array-into-equal-pairs/solution.py

Date: 2026-06-06

Time: 16:26

Purpose

This file solves LeetCode 2206 — Divide Array Into Equal Pairs. Given an array nums of length 2n, it determines whether the array can be divided into exactly n pairs where each pair consists of two equal elements. It owns the algorithmic logic and exposes it via the standard Solution class that the test harness expects.

Key Components

Solution.divideArrayIntoEqualPairs(self, nums: List[int]) -> bool — The sole method. It returns True if and only if every distinct value in nums appears an even number of times. The contract matches LeetCode's signature: takes a list of integers, returns a boolean.

Patterns

Dependencies

Imports:

Imported by: The test file at divide-array-into-equal-pairs/test_solution.py (and the "Imported By" list in the prompt is the full cross-repo test suite, which imports from a shared test infrastructure, not from this file specifically).

Flow

1. Counter(nums) builds a frequency map in O(n) time, O(k) space where k = distinct values.

2. The generator count % 2 == 0 for count in Counter(nums).values() lazily checks each frequency.

3. all(...) short-circuits on the first odd count, returning False. If every count is even, returns True.

Total: O(n) time, O(k) space. Single pass through the array, single pass through the counter.

Invariants

Error Handling

None. The method assumes valid input per LeetCode constraints (non-empty list of integers with even length). No exceptions are raised or caught.

Topics to Explore

Beliefs