#2404

Most Frequent Even Element

pupil · 420 · lc easy +25 · verified · 53.3% accepted · 1,117 likes · top 44%

Description

Given an integer array nums, find the most frequently occurring even element. If there is a tie, return the smallest such element. If no even elements exist, return -1.

Example 1:

Input: nums = [0,1,2,2,4,4,1]
Output: 2
Explanation:
The even elements are 0, 2, and 4. Of these, 2 and 4 appear the most.
We return the smallest one, which is 2.

Example 2:

Input: nums = [4,4,4,9,2,4]
Output: 4
Explanation: 4 is the even element appears the most.

Example 3:

Input: nums = [29,47,21,41,13,37,25,7]
Output: -1
Explanation: There is no even element.

Code

1
2
3