Skip to main content
Back to problems
#2206
Easy Algorithms

Divide array into equal pairs

Array Hash Table Bit Manipulation Counting
79.2% acceptance
Feb 25, 2026
1193
50
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.

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
use std::collections::HashMap;


impl Solution {
  pub fn divide_array(nums: Vec<i32>) -> bool {
    let mut freq: HashMap<i32, i32> = HashMap::new();
    for &n in &nums {
      *freq.entry(n).or_insert(0) += 1;
    }
    freq.values().all(|&v| v % 2 == 0)
  }
}