Find Lucky Integer in an Array

Easy

Description

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.

Examples

Input:arr = [2,2,3,4]
Output:2
Explanation:

2 appears 2 times.

Input:[1, 2, 2, 3, 3, 3]
Output:3
Explanation:

1 appears 1 time, 2 appears 2 times, and 3 appears 3 times. All three are lucky integers, but 3 is the largest.

Input:[5, 5, 5, 5, 7, 7, 7, 7, 7]
Output:-1
Explanation:

5 appears 4 times (not equal to 5) and 7 appears 5 times (not equal to 7). Since no integer has a frequency equal to its value, return -1.

Constraints

  • 1 ≤ arr.length ≤ 500
  • 1 ≤ arr[i] ≤ 500

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!