forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_2190.java
More file actions
26 lines (24 loc) · 735 Bytes
/
_2190.java
File metadata and controls
26 lines (24 loc) · 735 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
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.Map;
public class _2190 {
public static class Solution1 {
public int mostFrequent(int[] nums, int key) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] == key) {
map.put(nums[i + 1], map.getOrDefault(nums[i + 1], 0) + 1);
}
}
int most = 0;
int ans = 0;
for (int k : map.keySet()) {
if (map.get(k) > most) {
ans = k;
most = map.get(k);
}
}
return ans;
}
}
}