-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlucky_num_in_an_array.java
More file actions
37 lines (29 loc) · 1.01 KB
/
lucky_num_in_an_array.java
File metadata and controls
37 lines (29 loc) · 1.01 KB
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
37
import java.util.HashMap;
public class LuckyIntegerFinder {
public static int findLucky(int[] arr) {
HashMap<Integer, Integer> freq = new HashMap<>();
for (int num : arr) {
if (freq.containsKey(num)) {
freq.put(num, freq.get(num) + 1);
} else {
freq.put(num, 1);
}
}
int lucky = -1;
for (int num : freq.keySet()) {
int count = freq.get(num);
if (num == count) {
lucky = Math.max(lucky, num);
}
}
return lucky;
}
public static void main(String[] args) {
int[] arr1 = {2, 2, 3, 4}; // Output: 2
int[] arr2 = {1, 2, 2, 3, 3, 3}; // Output: 3
int[] arr3 = {2, 2, 2, 3, 3}; // Output: -1
System.out.println("Lucky in arr1: " + findLucky(arr1));
System.out.println("Lucky in arr2: " + findLucky(arr2));
System.out.println("Lucky in arr3: " + findLucky(arr3));
}
}