aboutsummaryrefslogtreecommitdiff
path: root/find_lucky_integer_array.py
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-07-04 18:49:43 -0600
committerJack Sangdahl <[email protected]>2025-07-04 18:49:43 -0600
commit321ae48f1860944804457bb0c8cc47ab0ebaaaec (patch)
tree166b937be6964aaa378b7137f286487ed026b226 /find_lucky_integer_array.py
parentcd518a4b7fb4fb8276751af7c0d7294389f34632 (diff)
downloadleetcode-321ae48f1860944804457bb0c8cc47ab0ebaaaec.tar.gz
leetcode-321ae48f1860944804457bb0c8cc47ab0ebaaaec.zip
1394. find lucky integer in an array
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]))