< Back





1394. Find Lucky Integer in an Array

A lucky integer is one that its interger value is the same as its frequency in the input array.
Return the largets lucky integer or, if there isn't one, return -1.

The solution is as follows:


  from collections import Counter

  class Solution:
      def findLucky(self, arr: List[int]) -> int:
          counts = Counter(arr)
          ans = -1

          for num, count in counts.items():
              if num == count:
                  ans = max(ans, num)

          return ans


_ Time Complexity:

  O(n) - We have to iterate through the entire input array.

_ Space Complexity:

  O(n) - We store the frequency of each number in the input array.