Skip to content

Latest commit

 

History

History
22 lines (20 loc) · 536 Bytes

2404.md

File metadata and controls

22 lines (20 loc) · 536 Bytes

2404. Most Frequent Even Element

Solution 1 (time O(n), space O(n))

class Solution(object):
    def mostFrequentEven(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        d = collections.Counter()
        for num in nums:
            if num % 2 == 0:
                d[num] += 1
        ans, cnt = -1, 0
        for k, v in d.items():
            if v > cnt or (v == cnt and k < ans):
                ans = k
                cnt = v
        return ans