2404-出现最频繁的偶数元素

Raphael Liu Lv10

给你一个整数数组 nums ,返回出现最频繁的偶数元素。

如果存在多个满足条件的元素,只需要返回 最小 的一个。如果不存在这样的元素,返回 -1

示例 1:

**输入:** nums = [0,1,2,2,4,4,1]
**输出:** 2
**解释:**
数组中的偶数元素为 0、2 和 4 ,在这些元素中,2 和 4 出现次数最多。
返回最小的那个,即返回 2 。

示例 2:

**输入:** nums = [4,4,4,9,2,4]
**输出:** 4
**解释:** 4 是出现最频繁的偶数元素。

示例 3:

**输入:** nums = [29,47,21,41,13,37,25,7]
**输出:** -1
**解释:** 不存在偶数元素。

提示:

  • 1 <= nums.length <= 2000
  • 0 <= nums[i] <= 105

方法一:哈希表计数

遍历数组 nums,并且使用哈希表 count 记录偶数元素的出现次数。使用 res 和 ct 分别记录当前出现次数最多的元素值以及对应的出现次数。遍历哈希表中的元素,如果元素的出现次数大于 ct 或者出现次数等于 ct 且元素值小于 res,那么用 res 记录当前遍历的元素值,并且用 ct 记录当前遍历的元素的出现次数。

[sol1-C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
int mostFrequentEven(vector<int>& nums) {
unordered_map<int, int> count;
for (auto x : nums) {
if (x % 2 == 0) {
count[x]++;
}
}
int res = -1, ct = 0;
for (auto &p : count) {
if (res == -1 || p.second > ct || p.second == ct && res > p.first) {
res = p.first;
ct = p.second;
}
}
return res;
}
};
[sol1-Java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public int mostFrequentEven(int[] nums) {
Map<Integer, Integer> count = new HashMap<Integer, Integer>();
for (int x : nums) {
if (x % 2 == 0) {
count.put(x, count.getOrDefault(x, 0) + 1);
}
}
int res = -1, ct = 0;
for (Map.Entry<Integer, Integer> entry : count.entrySet()) {
if (res == -1 || entry.getValue() > ct || entry.getValue() == ct && res > entry.getKey()) {
res = entry.getKey();
ct = entry.getValue();
}
}
return res;
}
}
[sol1-Python3]
1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
def mostFrequentEven(self, nums: List[int]) -> int:
count = Counter()
for x in nums:
if x % 2 == 0:
count[x] += 1
res, ct = -1, 0
for k, v in count.items():
if res == -1 or v > ct or (v == ct and res > k):
res = k
ct = v
return res
[sol1-C#]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Solution {
public int MostFrequentEven(int[] nums) {
IDictionary<int, int> count = new Dictionary<int, int>();
foreach (int x in nums) {
if (x % 2 == 0) {
count.TryAdd(x, 0);
count[x]++;
}
}
int res = -1, ct = 0;
foreach (KeyValuePair<int, int> pair in count) {
if (res == -1 || pair.Value > ct || pair.Value == ct && res > pair.Key) {
res = pair.Key;
ct = pair.Value;
}
}
return res;
}
}
[sol1-Golang]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
func mostFrequentEven(nums []int) int {
count := map[int]int{}
for _, x := range nums {
if x % 2 == 0 {
count[x]++
}
}
res, ct := -1, 0
for k, v := range count {
if res == -1 || ct < v || ct == v && k < res {
res = k
ct = v
}
}
return res
}
[sol1-JavaScript]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var mostFrequentEven = function(nums) {
let count = new Map();
for (let x of nums) {
if (x % 2 == 0) {
count.set(x, (count.get(x) || 0) + 1);
}
}
let res = -1, ct = 0;
for (let [k, v] of count) {
if (res == -1 || v > ct || v == ct && k < res) {
res = k;
ct = v;
}
}
return res;
};
[sol1-C]
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
typedef struct {
int key;
int val;
UT_hash_handle hh;
} HashItem;

HashItem *hashFindItem(HashItem **obj, int key) {
HashItem *pEntry = NULL;
HASH_FIND_INT(*obj, &key, pEntry);
return pEntry;
}

bool hashAddItem(HashItem **obj, int key, int val) {
if (hashFindItem(obj, key)) {
return false;
}
HashItem *pEntry = (HashItem *)malloc(sizeof(HashItem));
pEntry->key = key;
pEntry->val = val;
HASH_ADD_INT(*obj, key, pEntry);
return true;
}

bool hashSetItem(HashItem **obj, int key, int val) {
HashItem *pEntry = hashFindItem(obj, key);
if (!pEntry) {
hashAddItem(obj, key, val);
} else {
pEntry->val = val;
}
return true;
}

int hashGetItem(HashItem **obj, int key, int defaultVal) {
HashItem *pEntry = hashFindItem(obj, key);
if (!pEntry) {
return defaultVal;
}
return pEntry->val;
}

void hashFree(HashItem **obj) {
HashItem *curr = NULL, *tmp = NULL;
HASH_ITER(hh, *obj, curr, tmp) {
HASH_DEL(*obj, curr);
free(curr);
}
}

int mostFrequentEven(int* nums, int numsSize){
HashItem *count = NULL;
for (int i = 0; i < numsSize; i++) {
if (nums[i] % 2 == 0) {
hashSetItem(&count, nums[i], hashGetItem(&count, nums[i], 0) + 1);
}
}
int res = -1, ct = 0;
HashItem *pEntry = NULL;
for (pEntry = count; pEntry != NULL; pEntry = pEntry->hh.next) {
if (res == -1 || pEntry->val > ct || pEntry->val == ct && res > pEntry->key) {
res = pEntry->key;
ct = pEntry->val;
}
}
hashFree(&count);
return res;
}

复杂度分析

  • 时间复杂度:O(n),其中 n 是数组 nums 的长度。遍历数组与哈希表都需要 O(n)。

  • 空间复杂度:O(n)。保存哈希表需要 O(n)。

 Comments
On this page
2404-出现最频繁的偶数元素