|
| 1 | +# [2404. 出现最频繁的偶数元素](https://leetcode.cn/problems/most-frequent-even-element) |
| 2 | + |
| 3 | +[English Version](/solution/2400-2499/2404.Most%20Frequent%20Even%20Element/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>给你一个整数数组 <code>nums</code> ,返回出现最频繁的偶数元素。</p> |
| 10 | + |
| 11 | +<p>如果存在多个满足条件的元素,只需要返回 <strong>最小</strong> 的一个。如果不存在这样的元素,返回 <code>-1</code> 。</p> |
| 12 | + |
| 13 | +<p> </p> |
| 14 | + |
| 15 | +<p><strong>示例 1:</strong></p> |
| 16 | + |
| 17 | +<pre><strong>输入:</strong>nums = [0,1,2,2,4,4,1] |
| 18 | +<strong>输出:</strong>2 |
| 19 | +<strong>解释:</strong> |
| 20 | +数组中的偶数元素为 0、2 和 4 ,在这些元素中,2 和 4 出现次数最多。 |
| 21 | +返回最小的那个,即返回 2 。</pre> |
| 22 | + |
| 23 | +<p><strong>示例 2:</strong></p> |
| 24 | + |
| 25 | +<pre><strong>输入:</strong>nums = [4,4,4,9,2,4] |
| 26 | +<strong>输出:</strong>4 |
| 27 | +<strong>解释:</strong>4 是出现最频繁的偶数元素。 |
| 28 | +</pre> |
| 29 | + |
| 30 | +<p><strong>示例 3:</strong></p> |
| 31 | + |
| 32 | +<pre><strong>输入:</strong>nums = [29,47,21,41,13,37,25,7] |
| 33 | +<strong>输出:</strong>-1 |
| 34 | +<strong>解释:</strong>不存在偶数元素。 |
| 35 | +</pre> |
| 36 | + |
| 37 | +<p> </p> |
| 38 | + |
| 39 | +<p><strong>提示:</strong></p> |
| 40 | + |
| 41 | +<ul> |
| 42 | + <li><code>1 <= nums.length <= 2000</code></li> |
| 43 | + <li><code>0 <= nums[i] <= 10<sup>5</sup></code></li> |
| 44 | +</ul> |
| 45 | + |
| 46 | +## 解法 |
| 47 | + |
| 48 | +<!-- 这里可写通用的实现逻辑 --> |
| 49 | + |
| 50 | +**方法一:哈希表** |
| 51 | + |
| 52 | +用哈希表统计所有偶数元素出现的次数,然后找出出现次数最多且值最小的偶数元素。 |
| 53 | + |
| 54 | +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。 |
| 55 | + |
| 56 | +<!-- tabs:start --> |
| 57 | + |
| 58 | +### **Python3** |
| 59 | + |
| 60 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 61 | + |
| 62 | +```python |
| 63 | +class Solution: |
| 64 | + def mostFrequentEven(self, nums: List[int]) -> int: |
| 65 | + cnt = Counter(v for v in nums if v % 2 == 0) |
| 66 | + ans, mx = -1, 0 |
| 67 | + for v, t in cnt.items(): |
| 68 | + if mx < t or (mx == t and ans > v): |
| 69 | + mx = t |
| 70 | + ans = v |
| 71 | + return ans |
| 72 | +``` |
| 73 | + |
| 74 | +### **Java** |
| 75 | + |
| 76 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 77 | + |
| 78 | +```java |
| 79 | +class Solution { |
| 80 | + public int mostFrequentEven(int[] nums) { |
| 81 | + Map<Integer, Integer> cnt = new HashMap<>(); |
| 82 | + for (int v : nums) { |
| 83 | + if (v % 2 == 0) { |
| 84 | + cnt.put(v, cnt.getOrDefault(v, 0) + 1); |
| 85 | + } |
| 86 | + } |
| 87 | + int ans = -1, mx = 0; |
| 88 | + for (var e : cnt.entrySet()) { |
| 89 | + int v = e.getKey(), t = e.getValue(); |
| 90 | + if (mx < t || (mx == t && ans > v)) { |
| 91 | + mx = t; |
| 92 | + ans = v; |
| 93 | + } |
| 94 | + } |
| 95 | + return ans; |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +### **C++** |
| 101 | + |
| 102 | +```cpp |
| 103 | +class Solution { |
| 104 | +public: |
| 105 | + int mostFrequentEven(vector<int>& nums) { |
| 106 | + unordered_map<int, int> cnt; |
| 107 | + for (int v : nums) { |
| 108 | + if (v % 2 == 0) { |
| 109 | + ++cnt[v]; |
| 110 | + } |
| 111 | + } |
| 112 | + int ans = -1, mx = 0; |
| 113 | + for (auto [v, t] : cnt) { |
| 114 | + if (mx < t || (mx == t && ans > v)) { |
| 115 | + mx = t; |
| 116 | + ans = v; |
| 117 | + } |
| 118 | + } |
| 119 | + return ans; |
| 120 | + } |
| 121 | +}; |
| 122 | +``` |
| 123 | +
|
| 124 | +### **Go** |
| 125 | +
|
| 126 | +```go |
| 127 | +func mostFrequentEven(nums []int) int { |
| 128 | + cnt := map[int]int{} |
| 129 | + for _, v := range nums { |
| 130 | + if v%2 == 0 { |
| 131 | + cnt[v]++ |
| 132 | + } |
| 133 | + } |
| 134 | + ans, mx := -1, 0 |
| 135 | + for v, t := range cnt { |
| 136 | + if mx < t || (mx == t && ans > v) { |
| 137 | + mx = t |
| 138 | + ans = v |
| 139 | + } |
| 140 | + } |
| 141 | + return ans |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +### **TypeScript** |
| 146 | + |
| 147 | +```ts |
| 148 | + |
| 149 | +``` |
| 150 | + |
| 151 | +### **...** |
| 152 | + |
| 153 | +``` |
| 154 | +
|
| 155 | +
|
| 156 | +``` |
| 157 | + |
| 158 | +<!-- tabs:end --> |
0 commit comments