Skip to main content
Back to problems
#914
Easy Algorithms

X of a kind in a deck of cards

Array Hash Table Math Counting Number Theory
30.1% acceptance
Feb 25, 2026
1901
578
You are given an integer array deck where deck[i] represents the number written on the ith card. Partition the cards into one or more groups such that: Each group has exactly x cards where x > 1, and All the cards in one group have the same integer written on them. Return true if such partition is possible, or false otherwise.

Solution

Rust
Time O(2^n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn has_groups_size_x(deck: Vec<i32>) -> bool {
    fn gcd(a: i32, b: i32) -> i32 { if b == 0 { a } else { gcd(b, a % b) } }
    let mut map = std::collections::HashMap::new();
    for x in deck { *map.entry(x).or_insert(0) += 1; }
    let g = map.values().fold(0, |a, &b| gcd(a, b));
    g >= 2
  }
}