首页 > 其他分享 >[LeetCode] 1207. Unique Number of Occurrences

[LeetCode] 1207. Unique Number of Occurrences

时间:2022-11-30 06:44:05浏览次数:60  
标签:map arr false 1207 Occurrences Unique true LeetCode occurrences

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

独一无二的出现次数。

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

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

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/unique-number-of-occurrences
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这道题不难,但是近半年非常高频,就记录一下。题目问的是独一无二的出现次数,那么我们首先需要用 hashmap 记录所有出现过的元素和他们各自的出现次数。然后我们需要用一个 hashset 去过滤 map.values(),看看哪个出现次数是唯一的。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public boolean uniqueOccurrences(int[] arr) {
 3         HashMap<Integer, Integer> map = new HashMap<>();
 4         for (int num : arr) {
 5             map.put(num, map.getOrDefault(num, 0) + 1);
 6         }
 7         
 8         HashSet<Integer> set = new HashSet<>();
 9         for (int val : map.values()) {
10             if (!set.add(val)) {
11                 return false;
12             }
13         }
14         return true;
15     }
16 }

 

LeetCode 题目总结

标签:map,arr,false,1207,Occurrences,Unique,true,LeetCode,occurrences
From: https://www.cnblogs.com/cnoodle/p/16937310.html

相关文章

  • C++ folly库解读(三)Synchronized —— 比std::lock_guard/std::unique_lock更易用、功
    目录传统同步方案的缺点folly/Synchronized.h简单使用Synchronized的模板参数withLock()/withRLock()/withWLock()——更易用的加锁方式升级锁ulock()和withULo......
  • 62. Unique Paths
    Thereisarobotonan mxn grid.Therobotisinitiallylocatedatthe top-leftcorner (i.e., grid[0][0]).Therobottriestomovetothe bottom-right......
  • Typescript类型体操 - Unique
    题目中文实现类型的Lodash.uniq,Unique接受数组T,返回没有重复值的数组TEnglishImplementthetypeversionofLodash.uniq,UniquetakesanArrayT,returns......
  • 1207 独一无二的出现次数
    题目1207独一无二的出现次数给你一个整数数组arr,请你帮忙统计数组中每个数的出现次数。如果每个数的出现次数都是独一无二的,就返回true;否则返回false。示例1:输入......
  • Least Number of Unique Integers after K Removals
    https://leetcode.cn/problems/least-number-of-unique-integers-after-k-removals/ #https://leetcode.com/problems/least-number-of-unique-integers-after-k-remo......
  • c++11 unique_ptr
    unique_ptr使用详解      ......
  • C++(STL):05---智能指针之unique_ptr
    一、unique_ptr类头文件:#include<memory>智能指针,是一个模板。创建智能指针时,必须提供指针所指的类型与shared_ptr的不同之处:shared_ptr所指向的对象可以有多个其他shared_p......
  • MySQL数据库的唯一性约束(UNIQUE)
    一、数据库表的唯一性约束是什么MySQL唯一约束(UniqueKey)要求被约束的列中的数据唯一,允许为NULL,但只能出现一个NULL值。唯一约束可以确保一列或者几列不出现重复值。二、如......
  • MySQL的唯一约束(Unique Key),数据库设计必备
    一、数据库表的唯一性约束是什么MySQL唯一约束(UniqueKey)要求被约束的列中的数据唯一,允许为NULL,但只能出现一个NULL值。唯一约束可以确保一列或者几列不出现重复值。二、如......
  • C++——智能指针unique_ptr
    独占指针:unique_ptrunique_ptr在任何给定的时刻,只能有一个指针管理内存当指针超出作用域时,内存将自动释放该类型指针不可Copy,只可以Move运行结果没有运行delete......