aboutsummaryrefslogtreecommitdiff
path: root/find_lucky_integer_array.py
diff options
context:
space:
mode:
Diffstat (limited to 'find_lucky_integer_array.py')
-rw-r--r--find_lucky_integer_array.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/find_lucky_integer_array.py b/find_lucky_integer_array.py
new file mode 100644
index 0000000..078ea76
--- /dev/null
+++ b/find_lucky_integer_array.py
@@ -0,0 +1,28 @@
+# 1394. Find Lucky Integer in an Array
+from collections import Counter
+
+"""
+given an array of integers 'arr', a lucky integer is an integer equal 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 -, a lucky integer
+is an integer equal 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
+"""
+
+
+class Solution(object):
+ def findLucky(self, arr):
+ """
+ :type arr: List[int]
+ :rtype: int
+ """
+ cnt = Counter(arr)
+ return max([i for i, j in cnt.items() if i == j] + [-1])
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.findLucky(arr=[2, 2, 3, 4]))
+ print(obj.findLucky(arr=[1, 2, 2, 3, 3, 3]))
+ print(obj.findLucky(arr=[2, 2, 2, 3, 3]))