Skip to content

Commit e02c516

Browse files
Sync LeetCode submission Runtime - 173 ms (67.85%), Memory - 289.5 MB (84.18%)
1 parent 543f217 commit e02c516

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<p>You are given an integer array <code>nums</code>.</p>
2+
3+
<p>Return an integer denoting the <strong>first</strong> element (scanning from left to right) in <code>nums</code> whose <strong>frequency</strong> is <strong>unique</strong>. That is, no other integer appears the same number of times in <code>nums</code>. If there is no such element, return -1.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<div class="example-block">
9+
<p><strong>Input:</strong> <span class="example-io">nums = [20,10,30,30]</span></p>
10+
11+
<p><strong>Output:</strong> <span class="example-io">30</span></p>
12+
13+
<p><strong>Explanation:</strong></p>
14+
15+
<ul>
16+
<li>20 appears once.</li>
17+
<li>10 appears once.</li>
18+
<li>30 appears twice.</li>
19+
<li>The frequency of 30 is unique because no other integer appears exactly twice.</li>
20+
</ul>
21+
</div>
22+
23+
<p><strong class="example">Example 2:</strong></p>
24+
25+
<div class="example-block">
26+
<p><strong>Input:</strong> <span class="example-io">nums = [20,20,10,30,30,30]</span></p>
27+
28+
<p><strong>Output:</strong> <span class="example-io">20</span></p>
29+
30+
<p><strong>Explanation:</strong></p>
31+
32+
<ul>
33+
<li>20 appears twice.</li>
34+
<li>10 appears once.</li>
35+
<li>30 appears 3 times.</li>
36+
<li>The frequency of 20, 10, and 30 are unique. The first element that has unique frequency is 20.</li>
37+
</ul>
38+
</div>
39+
40+
<p><strong class="example">Example 3:</strong></p>
41+
42+
<div class="example-block">
43+
<p><strong>Input:</strong> <span class="example-io">nums = [10,10,20,20]</span></p>
44+
45+
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
46+
47+
<p><strong>Explanation:</strong></p>
48+
49+
<ul>
50+
<li>10 appears twice.</li>
51+
<li>20 appears twice.</li>
52+
<li>No element has a unique frequency.</li>
53+
</ul>
54+
</div>
55+
56+
<p>&nbsp;</p>
57+
<p><strong>Constraints:</strong></p>
58+
59+
<ul>
60+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
61+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
62+
</ul>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int firstUniqueFreq(vector<int>& nums) {
4+
unordered_map<int, int> freq;
5+
for (int i : nums) freq[i]++;
6+
unordered_map<int, int> freqB;
7+
for (auto i : freq) freqB[i.second]++;
8+
for (int i : nums) {
9+
if (freqB[freq[i]] == 1) return i;
10+
}
11+
return -1;
12+
}
13+
};

0 commit comments

Comments
 (0)