[LeetCode] Find Lucky Integer in an Array

1394. Find Lucky Integer in an Array

Given an array of integers arr, a lucky integer is an integer that has a frequency in the array equal to its value.

Return the largest lucky integer in the array. If there is no lucky integer return -1.

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
int findLucky(vector<int>& arr) {
unordered_map<int,int> freq;
for(auto& a : arr) freq[a]++;
int res = -1;
for(auto& [k,v] : freq) if(k == v) res = max(res, v);
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/07/05/PS/LeetCode/find-lucky-integer-in-an-array/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.