Easy

X of a Kind in a Deck of CardsPython

Full explanation · Time O(n * (logn)^2) · Space O(n)

# Time:  O(n * (logn)^2)
# Space: O(n)

import collections


class Solution(object):
    def hasGroupsSizeX(self, deck):
        """
        :type deck: List[int]
        :rtype: bool
        """
        def gcd(a, b):  # Time: O((logn)^2)
            while b:
                a, b = b, a % b
            return a

        vals = collections.Counter(deck).values()
        return reduce(gcd, vals) >= 2