#1207. Unique Number of Occurrences

ยท

1 min read

https://leetcode.com/problems/unique-number-of-occurrences/description/?envType=daily-question&envId=2024-01-17

var uniqueOccurrences = function(arr) {
    const counter =  {}, seen = new Set()

    for (const num of arr) {
        counter[num] = (counter[num] || 0) + 1
    }

    for (const count of Object.values(counter)) {
        if (seen.has(count)) return false
        seen.add(count)
    }

    return true

};

First, we count the total occurrence of each number in a counter object, then we extract the values of each number count using the Object.values(counter) since the counter keys will contain the numbers and the counter values will contain their count.

Then we check if any of the number's count equal another i.e. If duplicates exist, if it does, we return false in the loop, if it does not, then we return true at the end of the total check.

ย