|
| 1 | + |
| 2 | +# 1207. Unique Number of Occurrences |
| 3 | + |
| 4 | +> Difficulty: Easy |
| 5 | +> Tags: Array, Hash Table |
| 6 | +> Source: Weekly Contest 156 Q1 |
| 7 | +
|
| 8 | +## Problem |
| 9 | + |
| 10 | +Given an array of integers `arr`, return `true` if the number of occurrences of each value in the array is **unique**, or `false` otherwise. |
| 11 | + |
| 12 | +### Example 1: |
| 13 | + |
| 14 | +``` |
| 15 | +Input: arr = [1,2,2,1,1,3] |
| 16 | +Output: true |
| 17 | +Explanation: The value 1 has 3 occurrences, 2 has 2, and 3 has 1. No two values have the same number of occurrences. |
| 18 | +``` |
| 19 | + |
| 20 | +### Example 2: |
| 21 | + |
| 22 | +``` |
| 23 | +Input: arr = [1,2] |
| 24 | +Output: false |
| 25 | +``` |
| 26 | + |
| 27 | +### Example 3: |
| 28 | + |
| 29 | +``` |
| 30 | +Input: arr = [-3,0,1,-3,1,1,1,-3,10,0] |
| 31 | +Output: true |
| 32 | +``` |
| 33 | + |
| 34 | +## Constraints |
| 35 | + |
| 36 | +- `1 <= arr.length <= 1000` |
| 37 | +- `-1000 <= arr[i] <= 1000` |
| 38 | + |
| 39 | +--- |
| 40 | + |
| 41 | +## Approach |
| 42 | + |
| 43 | +The solution involves two main steps: |
| 44 | +1. Count the frequency of each element using a hash table. |
| 45 | +2. Check if the frequencies are unique by storing them in another hash table (or set). If there are any repeated frequencies, return `false`, otherwise return `true`. |
| 46 | + |
| 47 | +- **Time Complexity:** O(n), where `n` is the length of the array. |
| 48 | +- **Space Complexity:** O(n), for storing the frequency counts. |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +## Solutions |
| 53 | + |
| 54 | +### Python |
| 55 | + |
| 56 | +```python |
| 57 | +from collections import Counter |
| 58 | + |
| 59 | +class Solution: |
| 60 | + def uniqueOccurrences(self, arr: List[int]) -> bool: |
| 61 | + cnt = Counter(arr) |
| 62 | + return len(set(cnt.values())) == len(cnt) |
| 63 | +``` |
| 64 | + |
| 65 | +### Java |
| 66 | + |
| 67 | +```java |
| 68 | +import java.util.*; |
| 69 | + |
| 70 | +class Solution { |
| 71 | + public boolean uniqueOccurrences(int[] arr) { |
| 72 | + Map<Integer, Integer> cnt = new HashMap<>(); |
| 73 | + for (int x : arr) { |
| 74 | + cnt.merge(x, 1, Integer::sum); |
| 75 | + } |
| 76 | + return new HashSet<>(cnt.values()).size() == cnt.size(); |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +### C++ |
| 82 | + |
| 83 | +```cpp |
| 84 | +#include <unordered_map> |
| 85 | +#include <unordered_set> |
| 86 | +#include <vector> |
| 87 | +using namespace std; |
| 88 | + |
| 89 | +class Solution { |
| 90 | +public: |
| 91 | + bool uniqueOccurrences(vector<int>& arr) { |
| 92 | + unordered_map<int, int> cnt; |
| 93 | + for (int& x : arr) { |
| 94 | + ++cnt[x]; |
| 95 | + } |
| 96 | + unordered_set<int> vis; |
| 97 | + for (auto& [_, v] : cnt) { |
| 98 | + if (vis.count(v)) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + vis.insert(v); |
| 102 | + } |
| 103 | + return true; |
| 104 | + } |
| 105 | +}; |
| 106 | +``` |
| 107 | +
|
| 108 | +### Go |
| 109 | +
|
| 110 | +```go |
| 111 | +func uniqueOccurrences(arr []int) bool { |
| 112 | + cnt := map[int]int{} |
| 113 | + for _, x := range arr { |
| 114 | + cnt[x]++ |
| 115 | + } |
| 116 | + vis := map[int]bool{} |
| 117 | + for _, v := range cnt { |
| 118 | + if vis[v] { |
| 119 | + return false |
| 120 | + } |
| 121 | + vis[v] = true |
| 122 | + } |
| 123 | + return true |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +### TypeScript |
| 128 | + |
| 129 | +```ts |
| 130 | +function uniqueOccurrences(arr: number[]): boolean { |
| 131 | + const cnt: Map<number, number> = new Map(); |
| 132 | + for (const x of arr) { |
| 133 | + cnt.set(x, (cnt.get(x) || 0) + 1); |
| 134 | + } |
| 135 | + return cnt.size === new Set(cnt.values()).size; |
| 136 | +} |
| 137 | +``` |
0 commit comments