Find Lucky Integer in an Array LeetCode solution in Java
1class Solution {2public int findLucky(int[] arr) {3HashMap<Integer, Integer> map = new HashMap<>();4for (int i : arr) {5map.put(i, map.getOrDefault(i, 0) + 1);6}7System.out.print(map);8int max = 0;9for (Map.Entry<Integer, Integer> e : map.entrySet()) {10int temp = 0;11if (e.getKey() == (int) e.getValue()) {12temp = (int) e.getKey();13}14if (max < temp) {15max = temp;16}17}18if (max != 0) return max;19return -1;20}21}