LC.P2404[出现最频繁的偶数元素] 方法一:哈希表1234567891011121314151617class Solution { public int mostFrequentEven(int[] nums) { Map<Integer, Integer> map = new HashMap<>(); for (int num : nums) { if (num % 2 == 0) map.merge(num, 1, Integer::sum); } int ans = -1, max = 0; for (Map.Entry<Integer, Integer> entry : map.entrySet()) { int x = entry.getKey(), cnt = entry.getValue(); if ((max < cnt) || (max == cnt && ans > x)) { max = cnt; ans = x; } } return ans; }} 时间复杂度:$O(n)$ 空间复杂度:$O(n)$