[LeetCode]#1207. Unique Number of Occurrences

Fatboy Slim
1 min readMay 15, 2020

Environment: Python 3.7

Key technique: Counter,set

Given an array of integers arr, write a function that returns true if and only if the number of occurrences of each value in the array is unique.

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

Analysis:

  1. Use Counter to get values [1,1].
  2. Use set for [1,1] and output is {1}
  3. If step 1 and step 2 len is not the same. It is false.

Solution:

class Solution(object):
def uniqueOccurrences(self, arr):
counter = Counter(arr).values()
if(len(counter) == len(set(counter))):
return True
else:
return False

Submissions:

Reference:

https://leetcode.com/problems/unique-number-of-occurrences/discuss/578626/Faster-than-90-very-easy-to-understand-(3-line-code)

https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/368435/

--

--