[LeetCode] 1207. Unique Number of Occurrences(Kotlin)

0

LeetCode

목록 보기
44/58
post-thumbnail

[LeetCode] 1207. Unique Number of Occurrences(Kotlin)

풀이

import java.util.*

class Solution {
    fun uniqueOccurrences(arr: IntArray): Boolean {
        //numCount<arr에 나타난 숫자, arr에 나타난 횟수>
        var numCount = mutableMapOf<Int,Int>()
        for(num in arr){
            if(numCount.containsKey(num)){
                numCount[num] = numCount[num]!! + 1
            }
            else{
                numCount[num] = 1
            }
        }

        //숫자가 arr에 나타난 횟수가 unique한지 확인
        var numCountSet = mutableSetOf<Int>()
        for((num, count) in numCount){
            println("$num, $count")
            if (numCountSet.contains(count)) return false
            else numCountSet.add(count)
        }
        return true
    }
}
profile
Be able to be vulnerable, in search of truth

0개의 댓글