Skip to content

Commit 4630ad1

Browse files
authored
Create most-frequent-even-element.py
1 parent b2d4595 commit 4630ad1

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Python/most-frequent-even-element.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
import collections
5+
6+
7+
# freq table
8+
class Solution(object):
9+
def mostFrequentEven(self, nums):
10+
"""
11+
:type nums: List[int]
12+
:rtype: int
13+
"""
14+
cnt = collections.Counter(x for x in nums if x%2 == 0)
15+
return max(cnt.iterkeys(), key=lambda x: (cnt[x], -x)) if cnt else -1

0 commit comments

Comments
 (0)