Skip to content

Commit bea76fa

Browse files
authored
Create find-lucky-integer-in-an-array.cpp
1 parent 4961df9 commit bea76fa

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
int findLucky(vector<int>& arr) {
7+
const auto& count = counter(arr);
8+
int result = -1;
9+
for (const auto& [k, v] : count) {
10+
if (k == v) {
11+
result = max(result, k);
12+
}
13+
}
14+
return result;
15+
}
16+
17+
private:
18+
unordered_map<int, int> counter(const vector<int>& arr) {
19+
unordered_map<int, int> count;
20+
for (const auto& i : arr) {
21+
++count[i];
22+
}
23+
return count;
24+
}
25+
};

0 commit comments

Comments
 (0)