Find Lucky Integer in an Array - Problem
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.
Input & Output
Example 1 — Basic Lucky Number
$
Input:
arr = [2,2,3,4]
›
Output:
2
💡 Note:
Number 2 appears 2 times, so 2 is lucky. Number 3 appears 1 time (3 ≠ 1), number 4 appears 1 time (4 ≠ 1). Only 2 is lucky.
Example 2 — No Lucky Numbers
$
Input:
arr = [1,2,2,3,3,3]
›
Output:
-1
💡 Note:
Number 1 appears 1 time (1 = 1, lucky), number 2 appears 2 times (2 = 2, lucky), number 3 appears 3 times (3 = 3, lucky). The largest lucky number is 3. Wait, let me recalculate: 1→1 time (lucky), 2→2 times (lucky), 3→3 times (lucky). Largest is 3.
Example 3 — Multiple Lucky Numbers
$
Input:
arr = [7,7,7,7,7,7,7]
›
Output:
7
💡 Note:
Number 7 appears 7 times, so 7 is lucky. This is the only number and it's lucky.
Constraints
- 1 ≤ arr.length ≤ 500
- 1 ≤ arr[i] ≤ 500
Visualization
Tap to expand
Understanding the Visualization
1
Input Analysis
Array with integers, need to find numbers equal to their frequency
2
Count Frequencies
Use hash map to count how many times each number appears
3
Find Lucky Numbers
Check which numbers have frequency equal to their value
Key Takeaway
🎯 Key Insight: A number is lucky when its frequency in the array equals its value - use hash map counting to find them efficiently
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code