LeetCode in Kotlin

914. X of a Kind in a Deck of Cards

Easy

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:

Return true if such partition is possible, or false otherwise.

Example 1:

Input: deck = [1,2,3,4,4,3,2,1]

Output: true

Explanation:: Possible partition [1,1],[2,2],[3,3],[4,4].

Example 2:

Input: deck = [1,1,1,2,2,2,3,3]

Output: false

Explanation:: No possible partition.

Constraints:

Solution

class Solution {
    fun hasGroupsSizeX(deck: IntArray): Boolean {
        val map: HashMap<Int, Int> = HashMap()
        for (j in deck) {
            if (map.containsKey(j)) {
                map[j] = map.getValue(j) + 1
            } else {
                map[j] = 1
            }
        }
        var x = map.getValue(deck[0])
        for (entry in map.entries.iterator()) {
            x = gcd(x, entry.value)
        }
        return x >= 2
    }

    private fun gcd(a: Int, b: Int): Int {
        return if (b == 0) {
            a
        } else gcd(b, a % b)
    }
}