Find Lucky Integer in an Array

EasyArrays & Hashinghashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given an integer array arr, call a value **lucky** if the number of times it appears in arr is equal to the value itself. Return the largest lucky value in the array. If no value is lucky, return -1.

Examples

Input: arr = [2,2,3,4]
Output: 2
`2` appears exactly twice; `3` and `4` appear once.
Input: arr = [1,2,2,3,3,3]
Output: 3
`1`, `2` and `3` are all lucky, and `3` is the largest.
Input: arr = [2,2,2,3,3]
Output: -1
`2` appears three times and `3` appears twice, so neither is lucky.

Constraints

  • 1 <= arr.length <= 500
  • 1 <= arr[i] <= 500

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems