-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2404.cpp
More file actions
52 lines (41 loc) · 1.06 KB
/
Copy path2404.cpp
File metadata and controls
52 lines (41 loc) · 1.06 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
class Solution {
public:
int mostFrequentEven(vector<int>& nums) {
map<int, int> count;
for (int i : nums) {
if (i % 2 == 0) count[i]++;
}
if (count.empty()) return -1;
int res = -1;
int mFreq = 0;
for (auto [val, freq] : count) {
if (freq > mFreq) {
mFreq = freq;
res = val;
}
}
return res;
}
int newSol(vector<int>& nums) {
sort(nums.begin(), nums.end());
int res = -1, curr = nums[0], count = 0, max = 0;
for (int i=0; i<nums.size(); i++) {
if (nums[i] % 2 == 0) {
if (i>0 && nums[i] != nums[i-1]) {
curr = nums[i];
count = 0;
}
count++;
if (count > max) {
max = count;
res = nums[i];
}
}
}
return res;
}
};