-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuckyInteger.java
More file actions
37 lines (27 loc) · 918 Bytes
/
Copy pathLuckyInteger.java
File metadata and controls
37 lines (27 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package LeetcodePractice;
import java.util.*;
public class LuckyInteger {
public static int findLucky(int[] arr) {
Map<Integer, Integer> freqMap = new HashMap<>();
for (int num : arr) {
freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
}
int result = -1;
for (Map.Entry<Integer, Integer> entry : freqMap.entrySet()) {
int num = entry.getKey();
int freq = entry.getValue();
if (num == freq) {
result = Math.max(result, num);
}
}
return result;
}
public static void main(String[] args) {
int[] arr1 = {2, 2, 3, 4};
int[] arr2 = {1, 2, 2, 3, 3, 3};
int[] arr3 = {2, 2, 2, 3, 3};
System.out.println(findLucky(arr1));
System.out.println(findLucky(arr2));
System.out.println(findLucky(arr3));
}
}