1207. 独一无二的出现次数

  1. 独一无二的出现次数
    给你一个整数数组 arr,请你帮忙统计数组中每个数的出现次数。

如果每个数的出现次数都是独一无二的,就返回 true;否则返回 false。

示例 1:

输入:arr = [1,2,2,1,1,3]
输出:true
解释:在该数组中,1 出现了 3 次,2 出现了 2 次,3 只出现了 1 次。没有两个数的出现次数相同。
示例 2:

输入:arr = [1,2]
输出:false
示例 3:

输入:arr = [-3,0,1,-3,1,1,1,-3,10,0]
输出:true

class Solution {
    
    
    public boolean uniqueOccurrences(int[] arr) {
    
    
 Map<Integer, Integer>occur=new HashMap<Integer, Integer>();
		   for (int i : arr) {
    
    
			occur.put(i, occur.getOrDefault(i, 0)+1);
		}
		   Set<Integer> times=new HashSet<Integer>();
		   for(Map.Entry<Integer, Integer>x:occur.entrySet()) {
    
    
			   times.add(x.getValue());
		   }
		   return times.size()==occur.size();
    }
}
```[Java中map.getOrDefault()方法的使用](https://blog.csdn.net/a1439775520/article/details/104243690?utm_medium=distribute.pc_relevant.none-task-blog-title-1&spm=1001.2101.3001.4242)


猜你喜欢

转载自blog.csdn.net/weixin_45952706/article/details/109337804