# Leetcode [No. 1207] Unique Number of Occurences (EASY)
2024/01/17 Daily Challnge
## 題目
https://leetcode.com/problems/unique-number-of-occurrences/description/
## 思路
基本測資:
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.
這個解法很好想,我們先透過O(n)去把整個array都計數過一次記錄每一個數字出現的頻率,所以這個時候我們的第一個map會長這樣:
```=
num2freqs=
{
1:3 // 代表1出現三次
2:2
3:1
}
```
那接著我們再去iterate這個map,去建立第二個map
```=
freqs=
{
3:1 //代表"3"這個頻率出現一次,如果有其他數字出現的次數也是3,那這時候就會return false;
2:1
1:1
}
```
```c++=
class Solution {
public:
bool uniqueOccurrences(vector<int>& arr) {
unordered_map<int,int> num2freqs;
for (auto num : arr)
{
num2freqs[num]++;
}
// {1:3, 2:2, 3:1}
unordered_map<int,int> freqs;
// record the map with occurence and its count
// for example 1, the freqs map store {3:1, 2:1, 1:1}
for(auto [_ , cnt]: num2freqs)
{
if(freqs[cnt] > 0 )
{
return false;
}
freqs[cnt]++;
}
return true;
}
};
```
### 解法分析
+ time complexity: O(n)
### 執行結果
