09 Sept 2026Go / Python / TypeScriptEasy

X of a Kind in a Deck of Cards

Determine whether cards can be partitioned into equal-size groups of identical values with group size at least two.

Count each card value and compute the greatest common divisor of all frequencies; a valid shared group size exists exactly when the gcd exceeds one.

complexity

O(n + v log n) time and O(v) space for v distinct values.

solution files

  • Go x-of-a-kind-in-a-deck-of-cards/solution.go
  • Python x-of-a-kind-in-a-deck-of-cards/solution.py
  • TypeScript x-of-a-kind-in-a-deck-of-cards/solution.ts

Solution files

Gox-of-a-kind-in-a-deck-of-cards/solution.go
package main

func hasGroupsSizeX(deck []int) bool {
	counts := map[int]int{}
	for _, card := range deck {
		counts[card]++
	}
	gcd := func(a int, b int) int {
		for b != 0 {
			a, b = b, a%b
		}
		return a
	}
	divisor := 0
	for _, count := range counts {
		divisor = gcd(divisor, count)
	}
	return divisor >= 2
}
Pythonx-of-a-kind-in-a-deck-of-cards/solution.py
from collections import Counter
from functools import reduce
from math import gcd


class Solution:
    def hasGroupsSizeX(self, deck: list[int]) -> bool:
        return reduce(gcd, Counter(deck).values()) >= class="syntax-number">2
TypeScriptx-of-a-kind-in-a-deck-of-cards/solution.ts
function hasGroupsSizeX(deck: number[]): boolean {
  const counts = new Map<number, number>(); for (const card of deck) counts.set(card, (counts.get(card) ?? class="syntax-number">0) + class="syntax-number">1);
  const gcd = (a: number, b: number): number => b === class="syntax-number">0 ? a : gcd(b, a % b);
  let divisor = class="syntax-number">0; for (const count of counts.values()) divisor = gcd(divisor, count);
  return divisor >= class="syntax-number">2;
}