Skip to content

Commit 7fb3216

Browse files
authored
Create 2404-most-frequent-even-element.js
1 parent 71be2d9 commit 7fb3216

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

2404-most-frequent-even-element.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var mostFrequentEven = function(nums) {
6+
const hash = {}
7+
for(const e of nums) {
8+
if(e % 2 === 0) {
9+
if(hash[e] == null) hash[e] = 0
10+
hash[e]++
11+
}
12+
}
13+
const entries = Object.entries(hash)
14+
if(entries.length === 0) return -1
15+
entries.sort((a, b) => b[1] - a[1])
16+
const v = entries[0][1]
17+
const keys = Object.keys(hash).map(e => +e).sort((a, b) => a - b)
18+
// console.log(hash)
19+
for(const k of keys) {
20+
if(hash[k] === v) return k
21+
}
22+
23+
return -1
24+
};

0 commit comments

Comments
 (0)