方法一:遍历X
class Solution:
def hasGroupsSizeX(self, deck: List[int]) -> bool:
count = collections.Counter(deck)
n = len(deck)
for X in range(2, n+1):
if n % X == 0:
if all(v % X == 0 for v in count.values()):
return True
return False
```
方法二:利用最大公约数
```python
class Solution:
def hasGroupsSizeX(self, deck: List[int]) -> bool:
from fractions import gcd
vals = collections.Counter(deck).values()
return reduce(gcd, vals) >= 2```