Find Lucky Integer in an Array ( LeetCode ) Problem Solution.



Find Lucky Integer in an Array ( LeetCode ) Problem Solution.

Given an array of integers arr, a lucky integer is an integer which encompasses a frequency within the array up to its value.
Return a lucky integer within the array. If there are multiple lucky integers return the most important of them. If there's no lucky integer return -1.

Example 1:

Input: arr = [2,2,3,4]
Output: 2
Explanation: the sole lucky number within the array is 2 because frequency[2] == 2.

Example 2:
Input: arr = [1,2,2,3,3,3]
Output: 3
Explanation: 1, 2 and three are all lucky numbers, return the most important of them.

Example 3:
Input: arr = [2,2,2,3,3]
Output: -1
Explanation: There are not any lucky numbers within the array.

Example 4:
Input: arr = [5]
Output: -1

Example 5:


Input: arr = [7,7,7,7,7,7,7]
Output: 7

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


Solution :-


from collections import Counter

class Solution:

    def findLucky(self, arr: List[int]) -> int:

        counts=Counter(arr)

        print(counts.items())

        lucky=[elem for elem,frq in counts.items() if elem==frq ]

        

        return max(lucky) if lucky else -1

Runtime: 108 ms, faster than 17.93% of Python3 online submissions for Find Lucky Integer in an Array.
Memory Usage: 13.8 MBbut 100.00% of Python3 online submissions for Find Lucky Integer in an Array.

You May Also like:- How to create Tinder Swipe Cards in Flutter using 'flutter_tindercard' package ?

You may also like:- Create your first API using 'Deno' and fetch all the data from API in the Flutter app using 'http' package !!!