Skip to content

1394. Find Lucky Integer in an Array #1890

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to find the largest "lucky integer" in an array. A lucky integer is defined as an integer whose frequency in the array is exactly equal to its value. If no such integer exists, we should return -1.

Approach

  1. Frequency Counting: We first count the frequency of each integer in the array. Given the constraints (array length ≤ 500 and integers between 1 and 500), we can efficiently use an array of size 501 (to cover indices 1 through 500) to store these frequencies.
  2. Finding the Largest Lucky Integer: After counting the frequencies, we iterate from the largest possible integer (500) down to 1. For each integer, we check if its frequency matches its value. The first such integer encount…

Replies: 1 comment 2 replies

Comment options

mah-shamim
Jul 5, 2025
Maintainer Author

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Jul 5, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Jul 5, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants