Skip to main content

Most Frequent Even Element - Solution & Explanation

EasyArrayHash TableCounting15 min readAsked at: Google, Bloomberg
Practice this problem

Problem Statement

Given an integer array nums, return the most frequent even element.

If there is a tie, return the smallest one. If there is no such element, 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.

 

Constraints:

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

Approach Overview

Problem Overview: You are given an integer array and must return the even number that appears most frequently. If multiple even numbers share the same frequency, return the smallest one. If the array contains no even numbers, return -1.

Approach 1: Using HashMap to Count Frequencies (O(n) time, O(k) space)

Traverse the array once and track the frequency of each even number using a hash map. For every element, check num % 2 == 0 and increment its count in the map. After building the frequency table, iterate through the stored entries to find the number with the highest frequency, breaking ties by choosing the smaller value. Hash lookups and updates run in constant time, so the full scan stays linear. This approach works well for general cases and directly models the problem as a frequency counting task using a hash table.

Approach 2: Sorting and Counting Consecutive Elements (O(n log n) time, O(1) extra space)

First sort the array so identical values become consecutive. Then iterate through the sorted array while counting streaks of the same even number. When the value changes, compare the current streak length with the best frequency seen so far and update the result if needed. Sorting groups duplicates together, which makes counting simple without extra data structures. This approach trades additional time for minimal memory and relies mainly on array traversal and sequential counting.

Recommended for interviews: The hash map counting approach is typically expected. It runs in O(n) time and clearly demonstrates frequency tracking using a hash table. The sorting approach still works but is slower due to the O(n log n) sort step. Showing the counting logic first and then optimizing with a hash map demonstrates strong problem‑solving progression.

Approach 1: Using HashMap to Count Frequencies

This approach involves using a hash map or dictionary to count the frequency of each even number. We then iterate through the hash map to find the even number with the highest frequency, choosing the smallest one in case of a tie.

The code uses an array as a frequency map to count occurrences of each even element. We iterate over `nums`, update the frequency of even numbers, and track the maximum frequency and the smallest even number when frequencies match. Finally, the smallest number with the highest frequency is returned.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the length of the array because we iterate through the array once.
Space Complexity: O(100001), which is O(1) in constant space for a fixed-size array.

Try this approach in the editor →

Approach 2: Sorting and Counting Consecutive Elements

This approach involves sorting the array. After sorting, we iterate through the array and count consecutive even numbers, keeping track of the most frequent one. This method takes advantage of the sorting to make the counting simpler once elements are organized.

This C solution first sorts the array to organize the numbers. It then iterates over the sorted array, counting consecutive even numbers to determine which one appears most frequently. The condition checks whether to update the most frequent even number considering current frequency.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n log n) due to sorting, where n is the number of elements.
Space Complexity: O(1) additional space since we sort in place.

Try this approach in the editor →

Approach 3: 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.

The time complexity is O(n), and the space complexity is O(n). Here, n is the length of the array.

Code

Python

Java

C++

Go

TypeScript

Rust

PHP

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Using HashMap to Count Frequencies

Time Complexity: O(n), where n is the length of the array because we iterate through the array once.
Space Complexity: O(100001), which is O(1) in constant space for a fixed-size array.

Sorting and Counting Consecutive Elements

Time Complexity: O(n log n) due to sorting, where n is the number of elements.
Space Complexity: O(1) additional space since we sort in place.

Hash Table—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
HashMap Frequency CountingO(n)O(k)General case when you want the fastest linear scan solution
Sorting + Consecutive CountingO(n log n)O(1) extra (depending on sort)When avoiding extra hash map memory or when the array is already sorted

Video Solution

2404. Most Frequent Even Element | Leetcode Weekly Contest 310 | LeetCode 2404 • Bro Coders • 3,045 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Most Frequent Even Element easy or hard?
Most Frequent Even Element is classified as an Easy problem on LeetCode. It mainly tests basic array traversal, conditional filtering for even numbers, and frequency counting with a hash map.
Most Frequent Even Element Python/Java solution
In Python, use a dictionary or collections.Counter to track counts of even numbers while scanning the array. In Java, use a HashMap<Integer, Integer> to store frequencies and update the answer based on maximum count and smallest value tie-breaking.
How to solve Most Frequent Even Element in O(n)?
Filter even numbers during a single pass and store their counts in a hash map. After updating the frequency for each number, track the maximum count and choose the smallest value when frequencies tie. Hash table insert and lookup operations keep the overall complexity at O(n).
What is the best approach for Most Frequent Even Element?
The best approach is HashMap frequency counting. Iterate through the array, track counts of only even numbers in a hash table, and keep the number with the highest frequency. This method runs in O(n) time with O(k) space where k is the number of distinct even values.
Is Most Frequent Even Element asked at Google/Amazon/Meta?
This problem reflects a common interview pattern involving frequency counting and hash maps. Variations of the problem appear in coding interviews at companies like Amazon and Google where candidates must identify the most frequent element under certain constraints.
What data structure is used in Most Frequent Even Element?
A hash table (HashMap or dictionary) is the primary data structure used in the optimal solution. It stores each even number as a key and its occurrence count as the value, enabling constant-time updates and lookups during traversal.
What is the time complexity of Most Frequent Even Element?
The optimal solution runs in O(n) time because the array is scanned once while updating frequencies in a hash map. An alternative sorting-based solution takes O(n log n) time due to the sorting step before counting consecutive elements.

Ready to solve this problem?

Practice Most Frequent Even Element with our built-in code editor and test cases.

Practice on FleetCode