LeetCode Problem Workspace

Most Frequent Even Element

Identify the most frequent even element in an array using counting and hash lookup, handling ties by choosing the smallest number.

category

3

Topics

code_blocks

7

Code langs

hub

3

Related

Practice Focus

Easy · Array scanning plus hash lookup

bolt

Answer-first summary

Identify the most frequent even element in an array using counting and hash lookup, handling ties by choosing the smallest number.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Array scanning plus hash lookup

Try AiBox Copilotarrow_forward

Scan the array while tracking frequencies of even numbers in a hash table. After collecting counts, determine which even element appears most frequently, resolving ties by returning the smallest number. If no even elements exist, return -1 as required by the problem's constraints and expected behavior.

Problem Statement

Given an integer array nums, determine which even number occurs most frequently. If multiple even numbers share the highest frequency, return the smallest among them.

Return -1 if the array contains no even numbers. For example, in nums = [0,1,2,2,4,4,1], the even numbers 2 and 4 appear most often, so return 2 as the smallest.

Examples

Example 1

Input: nums = [0,1,2,2,4,4,1]

Output: 2

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

4 is the even element appears the most.

Example 3

Input: nums = [29,47,21,41,13,37,25,7]

Output: -1

There is no even element.

Constraints

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

Solution Approach

Frequency Counting with Hash Table

Iterate through nums and for each even number, update its count in a hash table. This ensures constant time lookups and accurate frequency tracking for the problem's core pattern.

Identify Maximum Frequency

After building the frequency map, scan all recorded even numbers to find the one with the highest count. In case of a tie, choose the smallest value to satisfy the problem's tie-breaking rule.

Return Result with Edge Case Handling

If the hash table is empty, return -1 to indicate no even elements. Otherwise, return the identified most frequent even number. This handles both normal and edge cases in one pass.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity is O(n) for scanning the array and updating the hash table. Space complexity is O(n) in the worst case if all elements are even and distinct, due to storing counts in the hash table.

What Interviewers Usually Probe

  • Ask if you can count frequencies instead of sorting to optimize performance.
  • Check how you handle ties and whether the smallest element is returned.
  • Consider edge cases with no even numbers to ensure robust solution.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to filter for even numbers before counting can lead to wrong results.
  • Neglecting the tie-breaker rule can cause failing outputs when frequencies match.
  • Returning 0 instead of -1 when no even numbers exist is a common oversight.

Follow-up variants

  • Most Frequent Odd Element: same pattern but filter for odd numbers instead.
  • Most Frequent Element Overall: count all elements without parity filtering.
  • Top K Frequent Even Elements: extend hash table to track top K frequencies instead of one.

FAQ

What is the main pattern used in Most Frequent Even Element?

The problem relies on array scanning combined with hash table counting to track frequencies efficiently.

How do I handle ties between multiple even numbers?

After counting, select the smallest number among those with the highest frequency as specified.

What should I return if there are no even numbers in the array?

Return -1 to indicate the absence of even elements.

Can this solution handle large arrays within constraints?

Yes, the O(n) time complexity and O(n) space for the hash table are suitable for arrays up to length 2000.

Is sorting necessary for this problem?

No, sorting is unnecessary; using a hash table to count frequencies is sufficient and more efficient.

terminal

Solution

Solution 1: Hash Table

We use a hash table $cnt$ to count the occurrence of all even elements, and then find the even element with the highest occurrence and the smallest value.

1
2
3
4
5
6
7
8
class Solution:
    def mostFrequentEven(self, nums: List[int]) -> int:
        cnt = Counter(x for x in nums if x % 2 == 0)
        ans, mx = -1, 0
        for x, v in cnt.items():
            if v > mx or (v == mx and ans > x):
                ans, mx = x, v
        return ans
Most Frequent Even Element Solution: Array scanning plus hash lookup | LeetCode #2404 Easy