Description

Given an array of integers arr, return true if the number of occurrences of each value in the array is unique or false otherwise.

Example 1:

  • Input: arr = [1,2,2,1,1,3]
  • Output: true
  • Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.

Example 2:

  • Input: arr = [1,2]
  • Output: false

Example 3:

  • Input: arr = [-3,0,1,-3,1,1,1,-3,10,0]
  • Output: true

Constraints:

  • 1 <= arr.length <= 1000
  • -1000 <= arr[i] <= 1000

💡 Hint 1:
Find the number of occurrences of each element in the array using a hash map.

💡 Hint 2:
Iterate through the hash map and check if there is a repeated value.

Submitted Code

class Solution:
    def uniqueOccurrences(self, arr: List[int]) -> bool:
        counter = {}
        occurrences = []                        # 등장 횟수 기록

        for n in arr:                           # 해시맵 생성
            counter[n] = counter.get(n, 0) + 1

        for v in counter.values():
            if v not in occurrences:
                occurrences.append(v)
            else:
                return False

        return True

Runtime: 0 ms | Beats 100.00%
Memory: 19.45 MB | Beats 15.00%

해시 테이블을 이용하는 간단한 문제다.

Other Solutions

1st

class Solution:
    def uniqueOccurrences(self, arr: List[int]) -> bool:
        freq = {}
        for x in arr:
            freq[x] = freq.get(x, 0) + 1

        return len(freq) == len(set(freq.values()))

time complexity: 𝑂(𝑛)
space complexity: 𝑂(𝑛)

Leave a comment