-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindLucky.java
More file actions
39 lines (30 loc) · 1.02 KB
/
FindLucky.java
File metadata and controls
39 lines (30 loc) · 1.02 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
38
39
import java.util.HashMap;
class FindLucky {
public int findLucky(int[] arr) {
HashMap<Integer, Integer> hm = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
int element = arr[i];
if (hm.containsKey(element)) {
hm.put(element, hm.get(element) + 1);
} else {
hm.put(element, 1);
}
}
int ans = -1;
for (int key : hm.keySet()) {
if (hm.get(key) == key) {
ans = Math.max(ans, key);
}
}
return ans;
}
public static void main(String[] args) {
FindLucky solution = new FindLucky();
int[] arr1 = {2, 2, 3, 4};
System.out.println(solution.findLucky(arr1)); // Output: 2
int[] arr2 = {1, 2, 2, 3, 3, 3};
System.out.println(solution.findLucky(arr2)); // Output: 3
int[] arr3 = {5, 5, 5, 5};
System.out.println(solution.findLucky(arr3)); // Output: -1
}
}