File tree Expand file tree Collapse file tree 6 files changed +185
-4
lines changed
solution/1200-1299/1207.Unique Number of Occurrences Expand file tree Collapse file tree 6 files changed +185
-4
lines changed Original file line number Diff line number Diff line change 39
39
<li><code>-1000 <= arr[i] <= 1000</code></li>
40
40
</ul >
41
41
42
-
43
42
## 解法
44
43
45
44
<!-- 这里可写通用的实现逻辑 -->
46
45
46
+ “哈希表 - 计数器”实现。
47
+
47
48
<!-- tabs:start -->
48
49
49
50
### ** Python3**
50
51
51
52
<!-- 这里可写当前语言的特殊实现逻辑 -->
52
53
53
54
``` 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
55
64
```
56
65
57
66
### ** Java**
58
67
59
68
<!-- 这里可写当前语言的特殊实现逻辑 -->
60
69
61
70
``` 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
+ ```
62
109
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
+ }
63
127
```
64
128
65
129
### ** ...**
Original file line number Diff line number Diff line change 36
36
<li><code>-1000 <= arr[i] <= 1000</code></li>
37
37
</ul >
38
38
39
-
40
39
## Solutions
41
40
42
41
<!-- tabs:start -->
43
42
44
43
### ** Python3**
45
44
46
45
``` 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
48
55
```
49
56
50
57
### ** Java**
51
58
52
59
``` 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
+ ```
53
98
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
+ }
54
116
```
55
117
56
118
### ** ...**
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments