Leetcode Note: Go - X of a Kind in a Deck of Cards

X of a Kind in a Deck of Cards - LeetCode
https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/

  • Go 言語で取り組んだメモ

回答

X of a Kind in a Deck of Cards - LeetCode
https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/solution/

Go GCD with explanation - LeetCode Discuss
https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/discuss/469539/Go-GCD-with-explanation

func hasGroupsSizeX(deck []int) bool {
    cnt := make(map[int]int, 0)
	for _, v := range deck {
		cnt[v]++
	}
    res:=cnt[deck[0]]
    for _, i:= range cnt{
        res = gcd(res, i)
    }
    return res>=2
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}