# LEETCODE 1394. Find Lucky Integer in an Array 解题思路分析

• 如果数组中存在多个幸运数，只需返回 最大 的那个。
• 如果数组中不含幸运数，则返回 -1

```输入：arr = [2,2,3,4]

```输入：arr = [1,2,2,3,3,3]

```输入：arr = [2,2,2,3,3]

```输入：arr = [5]

```输入：arr = [7,7,7,7,7,7,7]

• `1 <= arr.length <= 500`
• `1 <= arr[i] <= 500`

```public int findLucky(int[] arr) {
int[] count=new int[501];
int length=arr.length;
for(int num:arr){
if(num>length) continue;
count[num]++;
}
for(int i=length;i>=1;i--){
if(count[i]==i) return i;
}
return -1;
}```

Runtime: 1 ms, faster than 99.90% of Java online submissions for Find Lucky Integer in an Array.

Memory Usage: 39.4 MB, less than 100.00% of Java online submissions for Find Lucky Integer in an Array.

Kotlin版本：

```fun findLucky(arr: IntArray): Int {
var count = IntArray(501);
for(n in arr) count[n]++;
for(n in count.lastIndex downTo 1){
if(count[n]==n) return n;
}
return -1;
}```

Runtime: 188 ms, faster than 84.62% of Kotlin online submissions for Find Lucky Integer in an Array.

Memory Usage: 35 MB, less than 100.00% of Kotlin online submissions for Find Lucky Integer in an Array.