給你一個整數數組 arr,請你幫忙統計數組中每個數的出現次數。
如果每個數的出現次數都是獨一無二的,就返回 true;否則返回 false。
示例 1:
輸入:arr = [1,2,2,1,1,3]
輸出:true
解釋:在該數組中,1 出現了 3 次,2 出現了 2 次,3 只出現了 1 次。沒有兩個數的出現次數相同。
代碼
class Solution {public boolean uniqueOccurrences(int[] arr) {Set<Integer> set=new HashSet<>();Map<Integer,Integer> map=new HashMap<>();for(int i=0;i<arr.length;i++){map.put(arr[i],map.getOrDefault(arr[i],0)+1);//記錄每個數字出現的次數}for(int c:map.values())if(set.contains(c)) return false;//出現重復的次數else set.add(c);return true;}
}