We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4961df9 commit bea76faCopy full SHA for bea76fa
C++/find-lucky-integer-in-an-array.cpp
@@ -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