思路分析:
1、遍历数组,放入map中
2、key = 数组元素值、value = 该元素值出现的次数
3、注意复习HashMap的遍历方式(4种)
代码实现:
public class Test {
public static int fun(int[] nums) {
HashMap<Integer, Integer> map = new HashMap<>();
int n = nums.length;
for (int i = 0; i < n; i++) {
if (map.containsKey(nums[i])) {
map.put(nums[i], map.get(nums[i]) + 1);
} else {
map.put(nums[i], 1);
}
}
int res = 0;
int max = Integer.MIN_VALUE;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
res = entry.getKey();
max = entry.getValue();
}
}
return res;
}
public static void main(String[] args) {
int[] arr = {1, 5, 4, 3, 4, 4, 5, 4, 5, 5, 6, 6, 6, 6, 6};
System.out.println(fun(arr));
}
}