Find Lucky Integer in an Array
给一个数组, 定义一个lucky number是数组中这个数的频率和数字大小相同的数, 求最大的lucky number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Solution { public int findLucky(int[] arr) { int[] count = new int[501]; for(int n : arr) count[n]++; int max = Integer.MIN_VALUE; for(int i = arr.length - 1; i >= 0; i--){ if(count[arr[i]] == arr[i]) max = Math.max(max, arr[i]); } if(max == Integer.MIN_VALUE) return -1; else return max; } } |