Skip to main content
Back to problems
#1394
Easy Algorithms

Find lucky integer in an array

Array Hash Table Counting
75.5% acceptance
Feb 25, 2026
1618
45
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.

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn find_lucky(arr: Vec<i32>) -> i32 {
    let mut count = std::collections::HashMap::new();
    for x in &arr { *count.entry(x).or_insert(0) += 1; }
    let mut ans = -1;
    for (&val, &freq) in &count {
      if *val == freq && *val > ans { ans = *val; }
    }
    ans
  }
}