Skip to content

Commit c2f2654

Browse files
committed
feat: add solutions to lc problem: No.1207.Unique Number of Occurrences
1 parent a5c7fa2 commit c2f2654

File tree

6 files changed

+185
-4
lines changed

6 files changed

+185
-4
lines changed

solution/1200-1299/1207.Unique Number of Occurrences/README.md

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,91 @@
3939
<li><code>-1000 &lt;= arr[i] &lt;= 1000</code></li>
4040
</ul>
4141

42-
4342
## 解法
4443

4544
<!-- 这里可写通用的实现逻辑 -->
4645

46+
“哈希表 - 计数器”实现。
47+
4748
<!-- tabs:start -->
4849

4950
### **Python3**
5051

5152
<!-- 这里可写当前语言的特殊实现逻辑 -->
5253

5354
```python
54-
55+
class Solution:
56+
def uniqueOccurrences(self, arr: List[int]) -> bool:
57+
counter = collections.Counter(arr)
58+
s = set()
59+
for num in counter.values():
60+
if num in s:
61+
return False
62+
s.add(num)
63+
return True
5564
```
5665

5766
### **Java**
5867

5968
<!-- 这里可写当前语言的特殊实现逻辑 -->
6069

6170
```java
71+
class Solution {
72+
public boolean uniqueOccurrences(int[] arr) {
73+
Map<Integer, Integer> counter = new HashMap<>();
74+
for (int e : arr) {
75+
counter.put(e, counter.getOrDefault(e, 0) + 1);
76+
}
77+
Set<Integer> s = new HashSet<>();
78+
for (int num : counter.values()) {
79+
if (s.contains(num)) {
80+
return false;
81+
}
82+
s.add(num);
83+
}
84+
return true;
85+
}
86+
}
87+
```
88+
89+
### **C++**
90+
91+
```cpp
92+
class Solution {
93+
public:
94+
bool uniqueOccurrences(vector<int>& arr) {
95+
unordered_map<int, int> counter;
96+
for (auto e : arr) {
97+
++counter[e];
98+
}
99+
unordered_set<int> s;
100+
for (auto e : counter) {
101+
int num = e.second;
102+
if (s.count(num)) return false;
103+
s.insert(num);
104+
}
105+
return true;
106+
}
107+
};
108+
```
62109
110+
### **Go**
111+
112+
```go
113+
func uniqueOccurrences(arr []int) bool {
114+
counter := make(map[int]int)
115+
for _, e := range arr {
116+
counter[e]++
117+
}
118+
s := make(map[int]bool)
119+
for _, num := range counter {
120+
if s[num] {
121+
return false
122+
}
123+
s[num] = true
124+
}
125+
return true
126+
}
63127
```
64128

65129
### **...**

solution/1200-1299/1207.Unique Number of Occurrences/README_EN.md

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,83 @@
3636
<li><code>-1000 &lt;= arr[i] &lt;= 1000</code></li>
3737
</ul>
3838

39-
4039
## Solutions
4140

4241
<!-- tabs:start -->
4342

4443
### **Python3**
4544

4645
```python
47-
46+
class Solution:
47+
def uniqueOccurrences(self, arr: List[int]) -> bool:
48+
counter = collections.Counter(arr)
49+
s = set()
50+
for num in counter.values():
51+
if num in s:
52+
return False
53+
s.add(num)
54+
return True
4855
```
4956

5057
### **Java**
5158

5259
```java
60+
class Solution {
61+
public boolean uniqueOccurrences(int[] arr) {
62+
Map<Integer, Integer> counter = new HashMap<>();
63+
for (int e : arr) {
64+
counter.put(e, counter.getOrDefault(e, 0) + 1);
65+
}
66+
Set<Integer> s = new HashSet<>();
67+
for (int num : counter.values()) {
68+
if (s.contains(num)) {
69+
return false;
70+
}
71+
s.add(num);
72+
}
73+
return true;
74+
}
75+
}
76+
```
77+
78+
### **C++**
79+
80+
```cpp
81+
class Solution {
82+
public:
83+
bool uniqueOccurrences(vector<int>& arr) {
84+
unordered_map<int, int> counter;
85+
for (auto e : arr) {
86+
++counter[e];
87+
}
88+
unordered_set<int> s;
89+
for (auto e : counter) {
90+
int num = e.second;
91+
if (s.count(num)) return false;
92+
s.insert(num);
93+
}
94+
return true;
95+
}
96+
};
97+
```
5398
99+
### **Go**
100+
101+
```go
102+
func uniqueOccurrences(arr []int) bool {
103+
counter := make(map[int]int)
104+
for _, e := range arr {
105+
counter[e]++
106+
}
107+
s := make(map[int]bool)
108+
for _, num := range counter {
109+
if s[num] {
110+
return false
111+
}
112+
s[num] = true
113+
}
114+
return true
115+
}
54116
```
55117

56118
### **...**
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
bool uniqueOccurrences(vector<int>& arr) {
4+
unordered_map<int, int> counter;
5+
for (auto e : arr) {
6+
++counter[e];
7+
}
8+
unordered_set<int> s;
9+
for (auto e : counter) {
10+
int num = e.second;
11+
if (s.count(num)) return false;
12+
s.insert(num);
13+
}
14+
return true;
15+
}
16+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func uniqueOccurrences(arr []int) bool {
2+
counter := make(map[int]int)
3+
for _, e := range arr {
4+
counter[e]++
5+
}
6+
s := make(map[int]bool)
7+
for _, num := range counter {
8+
if s[num] {
9+
return false
10+
}
11+
s[num] = true
12+
}
13+
return true
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public boolean uniqueOccurrences(int[] arr) {
3+
Map<Integer, Integer> counter = new HashMap<>();
4+
for (int e : arr) {
5+
counter.put(e, counter.getOrDefault(e, 0) + 1);
6+
}
7+
Set<Integer> s = new HashSet<>();
8+
for (int num : counter.values()) {
9+
if (s.contains(num)) {
10+
return false;
11+
}
12+
s.add(num);
13+
}
14+
return true;
15+
}
16+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def uniqueOccurrences(self, arr: List[int]) -> bool:
3+
counter = collections.Counter(arr)
4+
s = set()
5+
for num in counter.values():
6+
if num in s:
7+
return False
8+
s.add(num)
9+
return True

0 commit comments

Comments
 (0)