Given an array of integers arr, a lucky integer is an integer which has a frequency in the array equal to its value.
Return a lucky integer in the array. If there are multiple lucky integers return the largest of them. If there is no lucky integer return -1.
 
Example 1:
Input: arr = [2,2,3,4]
Output: 2
Explanation: The only lucky number in the array is 2 because frequency[2] == 2.

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

Example 3:
Input: arr = [2,2,2,3,3]
Output: -1
Explanation: There are no lucky numbers in 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

Difficulty: EASY
Tags: Data structures

Mathematics
Skill types: Data structures

Starter code:
class Solution:
    def findLucky(self, arr: List[int]) -> int:
        

Solutions:
class Solution:

	def findLucky(self, arr: List[int]) -> int:
		c = collections.Counter(arr)
		maxi = -1
		for i in c:
			if i == c[i]:
				maxi = max(maxi, i)
		return maxi


from collections import Counter

class Solution:

	def findLucky(self, arr: List[int]) -> int:
		count = Counter(arr)
		lucky = [element for (element, frequency) in list(count.items()) if element == frequency]
		return max(lucky) if lucky else -1


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		largest = 0
		a = {}
		for i in arr:
			if i in a:
				a[i] = a[i] + 1
			else:
				a[i] = 1
		for k in a:
			if a[k] == k:
				if k > largest:
					largest = k
		return largest if largest else -1


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		dict = {}
		ans = -1
		for x in range(len(arr)):
			if arr[x] not in dict:
				dict[arr[x]] = 1
			else:
				dict[arr[x]] += 1
		for (key, value) in dict.items():
			if key == value:
				ans = max(ans, key)
		return ans


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		adict = {}
		i = 0
		j = len(arr)
		while i < j:
			if arr[i] in adict:
				print(arr[i], adict.get(arr[i]))
				adict[arr[i]] += 1
				print(arr[i], adict.get(arr[i]))
			else:
				adict[arr[i]] = 1
			i += 1
			output = -1
		for key in adict:
			if adict.get(key) == key:
				if adict.get(key) > output:
					output = adict.get(key)
		return output


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		book = {}.fromkeys(set(arr))
		for key in list(book.keys()):
			book[key] = arr.count(key)
		result = -1
		for (key, value) in list(book.items()):
			if key == value:
				result = max(result, key)
		return result


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		lucky = [-1]
		for (num, keys) in list(Counter(arr).items()):
			if num == keys:
				lucky.append(num)
		return max(lucky)


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		out = -1
		for (key, value) in list(Counter(arr).items()):
			if key == value and value > out:
				out = value
		return out


import collections

class Solution:

	def findLucky(self, arr):
		l = list((x for (x, y) in collections.Counter(arr).items() if x == y))
		return max(l) if l else -1


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		maximum = 0
		count = Counter(arr)
		for (key, value) in count.items():
			if key == value:
				if value > maximum:
					maximum = value
		if maximum != 0:
			return maximum
		return -1


class Solution:

	def findLucky(self, arr):
		dict_counts = {}
		for num in arr:
			if num in dict_counts:
				dict_counts[num] += 1
			else:
				dict_counts[num] = 1
		list_lukcy_nums = []
		for num in dict_counts:
			if num == dict_counts[num]:
				list_lukcy_nums.append(num)
		if len(list_lukcy_nums) > 0:
			return max(list_lukcy_nums)
		return -1


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		ans = -1
		for i in sorted(set(arr)):
			if i == arr.count(i):
				ans = i
		return ans


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		max1 = -1
		n = len(arr)
		for i in range(n):
			flag = False
			count = 0
			for j in reversed(range(0, i)):
				if arr[i] == arr[j]:
					flag = True
					break
			if flag == True:
				continue
			for j in range(i, n):
				if arr[j] == arr[i]:
					count += 1
			if arr[i] == count:
				if max1 < arr[i]:
					max1 = arr[i]
		return max1


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		lucky = -1
		for elem in set(arr):
			recurred = [e2 for e2 in arr if e2 == elem]
			if len(recurred) == elem:
				if elem > lucky:
					lucky = elem
		return lucky


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		arr.sort()
		luck = []
		for i in arr:
			if i == arr.count(i):
				luck.append(i)
		if len(luck) > 1:
			luck.sort()
			return luck[-1]
		if len(luck) == 1:
			return luck[0]
		return -1


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		arr.sort(reverse=True)
		for i in arr:
			if arr.count(i) == i:
				return i
		return -1


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		arr.sort()
		a = []
		for i in arr:
			if i == arr.count(i):
				a.append(i)
		if a:
			return max(a)
		else:
			return -1


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		arr.sort()
		l = []
		counts = 0
		visited = True
		for i in range(len(arr) - 1):
			counts = arr.count(arr[i])
			if counts == arr[i] and visited:
				l.append(arr[i])
				counts = 0
			if arr[i] == arr[i + 1]:
				visited = False
			else:
				visited = True
		if len(l) != 0:
			return max(l)
		else:
			return -1


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		arr.sort(reverse=True)
		freq = []
		luck = []
		for i in range(len(arr)):
			freq.append(arr.count(arr[i]))
			if freq[i] == arr[i]:
				luck.append(arr[i])
		max_luck = -1
		if luck:
			max_luck = max(luck)
		return max_luck


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		res = [num for num in arr if arr.count(num) == num]
		return max(res) if res else -1


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		luck = []
		for i in arr:
			if arr.count(i) == i:
				luck.append(i)
		if luck:
			return max(luck)
		return -1


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		nums = []
		for c in arr:
			if c == arr.count(c):
				nums.append(c)
		if len(nums) > 0:
			return max(nums)
		else:
			return -1


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		import collections
		return max([i for (i, j) in collections.Counter(arr).items() if i == j], default=-1)


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		num = -1
		for c in arr:
			if c == arr.count(c):
				num = max(num, c)
		return num


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		arr.sort(reverse=True)
		current_streak = 0
		for i in range(len(arr)):
			current_streak += 1
			if i == len(arr) - 1 or arr[i] != arr[i + 1]:
				if arr[i] == current_streak:
					return arr[i]
				current_streak = 0
		return -1


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		d = {}
		for i in arr:
			if i in d:
				d[i] = d[i] + 1
			else:
				d[i] = 1
		max = 0
		for (k, v) in list(d.items()):
			if k == v:
				if v > max:
					max = v
		if max != 0:
			return max
		else:
			return -1


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		arr.sort(reverse=True)
		count = 0
		temp = 501
		for i in range(len(arr)):
			print(temp, ',', count)
			if arr[i] != temp:
				if temp == count:
					return temp
				temp = arr[i]
				count = 1
			else:
				count += 1
			print(temp, ',,', count)
		if temp == count:
			return temp
		return -1


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		t = []
		x = collections.Counter(arr)
		for (j, v) in list(x.items()):
			if j == v:
				t.append(j)
		if len(t) != 0:
			return max(t)
		else:
			return -1


class Solution:

	def findLucky(self, arr: List[int]) -> int:
		lucky = -1
		op = {}
		for i in arr:
			if i not in op:
				op[i] = 1
			else:
				op[i] += 1
		for (k, v) in list(op.items()):
			if k == v:
				if k >= lucky:
					lucky = k
		return lucky