1394. Find Lucky Integer in an Array LeetCode Solution
In this guide, you will get 1394. Find Lucky Integer in an Array LeetCode Solution with the best time and space complexity. The solution to Find Lucky Integer in an Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find Lucky Integer in an Array solution in C++
- Find Lucky Integer in an Array solution in Java
- Find Lucky Integer in an Array solution in Python
- Additional Resources

Problem Statement of 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.
Example 1:
Input: arr = [2,2,3,4]
Output: 2
Explanation: The only lucky number in the array is 2 because frequency[2] == 2.
Example 2:
Input: arr = [1,2,2,3,3,3]
Output: 3
Explanation: 1, 2 and 3 are all lucky numbers, return the largest of them.
Example 3:
Input: arr = [2,2,2,3,3]
Output: -1
Explanation: There are no lucky numbers in the array.
Constraints:
1 <= arr.length <= 500
1 <= arr[i] <= 500
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
1394. Find Lucky Integer in an Array LeetCode Solution in C++
class Solution {
public:
int findLucky(vector<int>& arr) {
vector<int> count(arr.size() + 1);
for (const int a : arr)
if (a <= arr.size())
++count[a];
for (int i = arr.size(); i >= 1; --i)
if (count[i] == i)
return i;
return -1;
}
};
/* code provided by PROGIEZ */
1394. Find Lucky Integer in an Array LeetCode Solution in Java
class Solution {
public int findLucky(int[] arr) {
int[] count = new int[arr.length + 1];
for (final int a : arr)
if (a <= arr.length)
++count[a];
for (int i = arr.length; i >= 1; --i)
if (count[i] == i)
return i;
return -1;
}
}
// code provided by PROGIEZ
1394. Find Lucky Integer in an Array LeetCode Solution in Python
class Solution:
def findLucky(self, arr: list[int]) -> int:
count = [0] * (len(arr) + 1)
for a in arr:
if a <= len(arr):
count[a] += 1
for i in range(len(arr), 0, -1):
if count[i] == i:
return i
return -1
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.