Skip to main content

Find Lucky Integer in an Array - Solution & Explanation

EasyArrayHash TableCounting14 min readAsked at: Amazon, Microsoft, Meta +3
Practice this problem

Problem Statement

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

Approach Overview

Problem Overview: You are given an integer array arr. A number is called lucky if its value is equal to the number of times it appears in the array. Your task is to return the largest lucky integer. If no such number exists, return -1.

Approach 1: Using a Frequency Map (O(n) time, O(n) space)

Count how many times each number appears using a hash map. Iterate through the array once and update the frequency for each value. After building the map, iterate through the key-value pairs and check whether value == frequency. Track the maximum such value during the scan. Hash lookups and updates are constant time on average, making the overall complexity O(n). This approach works well for general cases and is the most common technique when solving frequency-based problems involving hash tables.

Approach 2: Using an Array to Track Frequencies (O(n) time, O(1) space)

The constraints limit values in arr to a small range, so you can replace the hash map with a counting array. Create a frequency array where the index represents the number and the value stores its count. Iterate through arr and increment the corresponding index. Then scan the frequency array and check which indices satisfy i == frequency[i], keeping the largest valid value. Since the array size is fixed by the constraint range, the extra space is effectively constant. This technique is a classic counting pattern frequently used in array problems.

Recommended for interviews: The frequency map approach is what most interviewers expect first. It demonstrates your ability to apply hash-based counting to reduce repeated scans. Mentioning the counting-array optimization shows deeper understanding of constraints and space tradeoffs, which signals stronger problem-solving skills.

Approach 1: Using a Frequency Map

This approach involves using a hash map (or dictionary) to store the frequency of each integer in the array. Once frequencies are calculated, iterate through the map to find integers whose value is equal to their frequency, and track the maximum of such values.

In this C solution, we use an array freq of size 501 to capture the frequency of numbers from 1 to 500. We then iterate over the original array updating our frequency array. Finally, we search for the maximum lucky number by checking if the frequency matches the number itself.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the length of the array.
Space Complexity: O(1), since the frequency array is of constant size (501).

Try this approach in the editor →

Approach 2: Using an Array to Track Frequencies

This approach involves using an array to directly track the frequency of each integer. By using an array of fixed size, we can avoid using a hash map or dictionary, which optimizes space usage when the domain of the input elements is known.

Similar to our first C solution but highlighting direct array access instead of hash maps, this implementation calculates frequency using a fixed-size array from 1 to 500. The result is determined by comparing each index with its frequency count directly.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the length of the array.
Space Complexity: O(1), using a constant-size array.

Try this approach in the editor →

Approach 3: Counting

We can use a hash table or an array cnt to count the occurrences of each number in arr. Then, we iterate through cnt to find the largest x such that cnt[x] = x. If there is no such x, return -1.

The time complexity is O(n), and the space complexity is O(n), where n is the length of the arr.

Code

Python

Java

C++

Go

TypeScript

Rust

PHP

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Using a Frequency Map

Time Complexity: O(n), where n is the length of the array.
Space Complexity: O(1), since the frequency array is of constant size (501).

Using an Array to Track Frequencies

Time Complexity: O(n), where n is the length of the array.
Space Complexity: O(1), using a constant-size array.

Counting—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Frequency Map (Hash Table)O(n)O(n)General case when counting occurrences of elements in an array
Counting Array (Frequency Array)O(n)O(1)When the value range is small and known in advance

Video Solution

LeetCode 1394 | Find Lucky Integer in an Array | Day 20 | 100 Days_LeetCode_Challenge|DSAwithedSlash • edSlash • 8,110 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Find Lucky Integer in an Array easy or hard?
Find Lucky Integer in an Array is classified as an Easy problem on LeetCode with a high acceptance rate. The challenge mainly tests whether you recognize the frequency counting pattern and apply a hash map or counting array efficiently.
How to solve Find Lucky Integer in an Array in O(n)?
Traverse the array and maintain a frequency count using a hash map or counting array. After counting, iterate through the frequencies and check whether any number appears exactly the same number of times as its value. Track the maximum such number while scanning. This approach ensures linear O(n) time.
What is the best approach for Find Lucky Integer in an Array?
The most practical approach uses a hash map to count the frequency of each number. After building the frequency map in one pass, scan the entries and check which values satisfy value == frequency. This solution runs in O(n) time with O(n) space and is the method most interviewers expect.
What data structure is used in Find Lucky Integer in an Array?
The core data structure is a hash table used to store element frequencies. In cases where the value range is small, a counting array can replace the hash map for better space efficiency while maintaining O(n) time complexity.
What is the time complexity of Find Lucky Integer in an Array?
The optimal solution runs in O(n) time because the array is scanned once to build a frequency count and once more to evaluate lucky numbers. Hash map operations such as insert and lookup are O(1) on average. Space complexity is O(n) for the frequency map.
Find Lucky Integer in an Array Python or Java solution approach
Both Python and Java implementations typically use a dictionary or HashMap to count frequencies. After building the map, iterate through its entries and return the largest key where key == frequency. The logic remains identical across languages and runs in O(n) time.
Is Find Lucky Integer in an Array asked at Google, Amazon, or Meta?
This problem represents a common interview pattern involving frequency counting and hash tables. Variants of this pattern frequently appear in interviews at companies like Amazon and Google, especially in easy-to-medium array and hashing rounds.

Ready to solve this problem?

Practice Find Lucky Integer in an Array with our built-in code editor and test cases.

Practice on FleetCode