Skip to content

Commit e1b448c

Browse files
committed
feat: update solutions to lc problem: No.2404
No.2404.Most Frequent Even Element
1 parent 318797d commit e1b448c

File tree

8 files changed

+140
-170
lines changed

8 files changed

+140
-170
lines changed

solution/2400-2499/2404.Most Frequent Even Element/README.md

+47-57
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
**方法一:哈希表**
5151

52-
用哈希表统计所有偶数元素出现的次数,然后找出出现次数最多且值最小的偶数元素。
52+
我们用哈希表 $cnt$ 统计所有偶数元素出现的次数,然后找出出现次数最多且值最小的偶数元素。
5353

5454
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。
5555

@@ -62,12 +62,11 @@
6262
```python
6363
class Solution:
6464
def mostFrequentEven(self, nums: List[int]) -> int:
65-
cnt = Counter(v for v in nums if v % 2 == 0)
65+
cnt = Counter(x for x in nums if x % 2 == 0)
6666
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
67+
for x, v in cnt.items():
68+
if v > mx or (v == mx and ans > x):
69+
ans, mx = x, v
7170
return ans
7271
```
7372

@@ -79,17 +78,17 @@ class Solution:
7978
class Solution {
8079
public int mostFrequentEven(int[] nums) {
8180
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);
81+
for (int x : nums) {
82+
if (x % 2 == 0) {
83+
cnt.merge(x, 1, Integer::sum);
8584
}
8685
}
8786
int ans = -1, mx = 0;
8887
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;
88+
int x = e.getKey(), v = e.getValue();
89+
if (mx < v || (mx == v && ans > x)) {
90+
ans = x;
91+
mx = v;
9392
}
9493
}
9594
return ans;
@@ -104,16 +103,16 @@ class Solution {
104103
public:
105104
int mostFrequentEven(vector<int>& nums) {
106105
unordered_map<int, int> cnt;
107-
for (int v : nums) {
108-
if (v % 2 == 0) {
109-
++cnt[v];
106+
for (int x : nums) {
107+
if (x % 2 == 0) {
108+
++cnt[x];
110109
}
111110
}
112111
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;
112+
for (auto& [x, v] : cnt) {
113+
if (mx < v || (mx == v && ans > x)) {
114+
ans = x;
115+
mx = v;
117116
}
118117
}
119118
return ans;
@@ -126,16 +125,15 @@ public:
126125
```go
127126
func mostFrequentEven(nums []int) int {
128127
cnt := map[int]int{}
129-
for _, v := range nums {
130-
if v%2 == 0 {
131-
cnt[v]++
128+
for _, x := range nums {
129+
if x%2 == 0 {
130+
cnt[x]++
132131
}
133132
}
134133
ans, mx := -1, 0
135-
for v, t := range cnt {
136-
if mx < t || (mx == t && ans > v) {
137-
mx = t
138-
ans = v
134+
for x, v := range cnt {
135+
if mx < v || (mx == v && x < ans) {
136+
ans, mx = x, v
139137
}
140138
}
141139
return ans
@@ -146,25 +144,21 @@ func mostFrequentEven(nums []int) int {
146144

147145
```ts
148146
function mostFrequentEven(nums: number[]): number {
149-
const map = new Map();
150-
for (const num of nums) {
151-
if (num % 2 === 0) {
152-
map.set(num, (map.get(num) ?? 0) + 1);
147+
const cnt: Map<number, number> = new Map();
148+
for (const x of nums) {
149+
if (x % 2 === 0) {
150+
cnt.set(x, (cnt.get(x) ?? 0) + 1);
153151
}
154152
}
155-
if (map.size === 0) {
156-
return -1;
157-
}
158-
159-
let res = 0;
160-
let max = 0;
161-
for (const [k, v] of map.entries()) {
162-
if (v > max || (v == max && k < res)) {
163-
max = v;
164-
res = k;
153+
let ans = -1;
154+
let mx = 0;
155+
for (const [x, v] of cnt) {
156+
if (mx < v || (mx == v && ans > x)) {
157+
ans = x;
158+
mx = v;
165159
}
166160
}
167-
return res;
161+
return ans;
168162
}
169163
```
170164

@@ -174,25 +168,21 @@ function mostFrequentEven(nums: number[]): number {
174168
use std::collections::HashMap;
175169
impl Solution {
176170
pub fn most_frequent_even(nums: Vec<i32>) -> i32 {
177-
let mut map = HashMap::new();
178-
for &num in nums.iter() {
179-
if num % 2 == 0 {
180-
*map.entry(num).or_insert(0) += 1;
171+
let mut cnt = HashMap::new();
172+
for &x in nums.iter() {
173+
if x % 2 == 0 {
174+
*cnt.entry(x).or_insert(0) += 1;
181175
}
182176
}
183-
if map.len() == 0 {
184-
return -1;
185-
}
186-
187-
let mut res = 0;
188-
let mut max = 0;
189-
for (&k, &v) in map.iter() {
190-
if v > max || (v == max && k < res) {
191-
max = v;
192-
res = k;
177+
let mut ans = -1;
178+
let mut mx = 0;
179+
for (&x, &v) in cnt.iter() {
180+
if mx < v || (mx == v && ans > x) {
181+
ans = x;
182+
mx = v;
193183
}
194184
}
195-
res
185+
ans
196186
}
197187
}
198188
```

solution/2400-2499/2404.Most Frequent Even Element/README_EN.md

+46-56
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,11 @@ We return the smallest one, which is 2.</pre>
5151
```python
5252
class Solution:
5353
def mostFrequentEven(self, nums: List[int]) -> int:
54-
cnt = Counter(v for v in nums if v % 2 == 0)
54+
cnt = Counter(x for x in nums if x % 2 == 0)
5555
ans, mx = -1, 0
56-
for v, t in cnt.items():
57-
if mx < t or (mx == t and ans > v):
58-
mx = t
59-
ans = v
56+
for x, v in cnt.items():
57+
if v > mx or (v == mx and ans > x):
58+
ans, mx = x, v
6059
return ans
6160
```
6261

@@ -66,17 +65,17 @@ class Solution:
6665
class Solution {
6766
public int mostFrequentEven(int[] nums) {
6867
Map<Integer, Integer> cnt = new HashMap<>();
69-
for (int v : nums) {
70-
if (v % 2 == 0) {
71-
cnt.put(v, cnt.getOrDefault(v, 0) + 1);
68+
for (int x : nums) {
69+
if (x % 2 == 0) {
70+
cnt.merge(x, 1, Integer::sum);
7271
}
7372
}
7473
int ans = -1, mx = 0;
7574
for (var e : cnt.entrySet()) {
76-
int v = e.getKey(), t = e.getValue();
77-
if (mx < t || (mx == t && ans > v)) {
78-
mx = t;
79-
ans = v;
75+
int x = e.getKey(), v = e.getValue();
76+
if (mx < v || (mx == v && ans > x)) {
77+
ans = x;
78+
mx = v;
8079
}
8180
}
8281
return ans;
@@ -91,16 +90,16 @@ class Solution {
9190
public:
9291
int mostFrequentEven(vector<int>& nums) {
9392
unordered_map<int, int> cnt;
94-
for (int v : nums) {
95-
if (v % 2 == 0) {
96-
++cnt[v];
93+
for (int x : nums) {
94+
if (x % 2 == 0) {
95+
++cnt[x];
9796
}
9897
}
9998
int ans = -1, mx = 0;
100-
for (auto [v, t] : cnt) {
101-
if (mx < t || (mx == t && ans > v)) {
102-
mx = t;
103-
ans = v;
99+
for (auto& [x, v] : cnt) {
100+
if (mx < v || (mx == v && ans > x)) {
101+
ans = x;
102+
mx = v;
104103
}
105104
}
106105
return ans;
@@ -113,16 +112,15 @@ public:
113112
```go
114113
func mostFrequentEven(nums []int) int {
115114
cnt := map[int]int{}
116-
for _, v := range nums {
117-
if v%2 == 0 {
118-
cnt[v]++
115+
for _, x := range nums {
116+
if x%2 == 0 {
117+
cnt[x]++
119118
}
120119
}
121120
ans, mx := -1, 0
122-
for v, t := range cnt {
123-
if mx < t || (mx == t && ans > v) {
124-
mx = t
125-
ans = v
121+
for x, v := range cnt {
122+
if mx < v || (mx == v && x < ans) {
123+
ans, mx = x, v
126124
}
127125
}
128126
return ans
@@ -133,25 +131,21 @@ func mostFrequentEven(nums []int) int {
133131

134132
```ts
135133
function mostFrequentEven(nums: number[]): number {
136-
const map = new Map();
137-
for (const num of nums) {
138-
if (num % 2 === 0) {
139-
map.set(num, (map.get(num) ?? 0) + 1);
134+
const cnt: Map<number, number> = new Map();
135+
for (const x of nums) {
136+
if (x % 2 === 0) {
137+
cnt.set(x, (cnt.get(x) ?? 0) + 1);
140138
}
141139
}
142-
if (map.size === 0) {
143-
return -1;
144-
}
145-
146-
let res = 0;
147-
let max = 0;
148-
for (const [k, v] of map.entries()) {
149-
if (v > max || (v == max && k < res)) {
150-
max = v;
151-
res = k;
140+
let ans = -1;
141+
let mx = 0;
142+
for (const [x, v] of cnt) {
143+
if (mx < v || (mx == v && ans > x)) {
144+
ans = x;
145+
mx = v;
152146
}
153147
}
154-
return res;
148+
return ans;
155149
}
156150
```
157151

@@ -161,25 +155,21 @@ function mostFrequentEven(nums: number[]): number {
161155
use std::collections::HashMap;
162156
impl Solution {
163157
pub fn most_frequent_even(nums: Vec<i32>) -> i32 {
164-
let mut map = HashMap::new();
165-
for &num in nums.iter() {
166-
if num % 2 == 0 {
167-
*map.entry(num).or_insert(0) += 1;
158+
let mut cnt = HashMap::new();
159+
for &x in nums.iter() {
160+
if x % 2 == 0 {
161+
*cnt.entry(x).or_insert(0) += 1;
168162
}
169163
}
170-
if map.len() == 0 {
171-
return -1;
172-
}
173-
174-
let mut res = 0;
175-
let mut max = 0;
176-
for (&k, &v) in map.iter() {
177-
if v > max || (v == max && k < res) {
178-
max = v;
179-
res = k;
164+
let mut ans = -1;
165+
let mut mx = 0;
166+
for (&x, &v) in cnt.iter() {
167+
if mx < v || (mx == v && ans > x) {
168+
ans = x;
169+
mx = v;
180170
}
181171
}
182-
res
172+
ans
183173
}
184174
}
185175
```

solution/2400-2499/2404.Most Frequent Even Element/Solution.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ class Solution {
22
public:
33
int mostFrequentEven(vector<int>& nums) {
44
unordered_map<int, int> cnt;
5-
for (int v : nums) {
6-
if (v % 2 == 0) {
7-
++cnt[v];
5+
for (int x : nums) {
6+
if (x % 2 == 0) {
7+
++cnt[x];
88
}
99
}
1010
int ans = -1, mx = 0;
11-
for (auto [v, t] : cnt) {
12-
if (mx < t || (mx == t && ans > v)) {
13-
mx = t;
14-
ans = v;
11+
for (auto& [x, v] : cnt) {
12+
if (mx < v || (mx == v && ans > x)) {
13+
ans = x;
14+
mx = v;
1515
}
1616
}
1717
return ans;

solution/2400-2499/2404.Most Frequent Even Element/Solution.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
func mostFrequentEven(nums []int) int {
22
cnt := map[int]int{}
3-
for _, v := range nums {
4-
if v%2 == 0 {
5-
cnt[v]++
3+
for _, x := range nums {
4+
if x%2 == 0 {
5+
cnt[x]++
66
}
77
}
88
ans, mx := -1, 0
9-
for v, t := range cnt {
10-
if mx < t || (mx == t && ans > v) {
11-
mx = t
12-
ans = v
9+
for x, v := range cnt {
10+
if mx < v || (mx == v && x < ans) {
11+
ans, mx = x, v
1312
}
1413
}
1514
return ans

0 commit comments

Comments
 (0)