class Solution:
def findLucky(self, arr):
from collections import Counter
freq = Counter(arr)
lucky = -1
for num, count in freq.items():
if num == count:
lucky = max(lucky, num)
return lucky
class Solution {
public:
int findLucky(vector<int>& arr) {
unordered_map<int, int> freq;
for (int n : arr) freq[n]++;
int lucky = -1;
for (auto& p : freq) {
if (p.first == p.second) lucky = max(lucky, p.first);
}
return lucky;
}
};
class Solution {
public int findLucky(int[] arr) {
Map<Integer, Integer> freq = new HashMap<>();
for (int n : arr) freq.put(n, freq.getOrDefault(n, 0) + 1);
int lucky = -1;
for (int key : freq.keySet()) {
if (key == freq.get(key)) lucky = Math.max(lucky, key);
}
return lucky;
}
}
var findLucky = function(arr) {
let freq = {};
for (let n of arr) freq[n] = (freq[n] || 0) + 1;
let lucky = -1;
for (let num in freq) {
if (parseInt(num) === freq[num]) lucky = Math.max(lucky, parseInt(num));
}
return lucky;
};
Given an array of integers arr
, find the largest "lucky" integer in the array. A lucky integer is defined as an integer whose value is exactly equal to its frequency in the array (i.e., the number appears in the array exactly as many times as its value). If there is no lucky integer, return -1
.
Key constraints:
-1
.arr = [2,2,3,4]
, 2
appears twice and 2 == 2
, so 2
is a lucky integer.
The core of the problem is to find an integer in the array such that its value is equal to the number of times it appears.
The brute-force way would be to check, for every unique number in the array, how many times it occurs, and then see if that count matches the number itself. However, this would require nested loops and be inefficient for large arrays.
To optimize, we can use a frequency map (hash table) to count the occurrences of each number in a single pass. This way, we can quickly check for each unique number if its frequency equals its value. Since we want the largest such integer, we keep track of the maximum one found.
This shift from brute-force to using a hash map is key for efficiency, reducing repeated counting and speeding up the solution.
We'll solve the problem with these clear steps:
-1
. Otherwise, return the largest lucky integer found.
We use a hash map because it allows us to count frequencies and check values in constant time per operation, making the algorithm efficient.
Let's walk through an example with arr = [2,2,3,4]
:
If the input was arr = [1,2,2,3,3,3]
:
Brute-force approach:
The optimized approach is much faster and uses extra space for the frequency map, but this is acceptable for most applications.
To solve the "Find Lucky Integer in an Array" problem, we leveraged a hash map to efficiently count the frequency of each integer. By comparing each number's value to its frequency, we identified all "lucky" integers and selected the largest one. This approach is both time and space efficient, and demonstrates the power of hash maps for frequency counting problems. The solution is simple, elegant, and scales well even for large arrays.