当前位置:网站首页>Unique occurrence times of leetcode simple questions

Unique occurrence times of leetcode simple questions

2022-07-05 22:03:00 ·Starry Sea

subject

Give you an array of integers arr, Please help count the number of occurrences of each number in the array .
If the number of occurrences of each number is unique , Just go back to true; Otherwise return to false.
Example 1:
Input :arr = [1,2,2,1,1,3]
Output :true
explain : In this array ,1 There is 3 Time ,2 There is 2 Time ,3 Only appeared. 1 Time . No two numbers appear the same number .
Example 2:
Input :arr = [1,2]
Output :false
Example 3:
Input :arr = [-3,0,1,-3,1,1,1,-3,10,0]
Output :true
Tips :
1 <= arr.length <= 1000
-1000 <= arr[i] <= 1000
source : Power button (LeetCode)

Their thinking

   First, count the frequency of elements , Then the frequency is de duplicated , If there is no change after weight removal , Then the number of occurrences is unique .

class Solution:
    def uniqueOccurrences(self, arr: List[int]) -> bool:
        return len(set(Counter(arr).values()))==len(Counter(arr))

 Insert picture description here

原网站

版权声明
本文为[·Starry Sea]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140503496625.html