Most Frequent Even Element - Solution & Explanation
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 <= 20000 <= 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.
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.
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.
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.
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.
Complexity Comparison
| Approach | Complexity |
|---|---|
| Using HashMap to Count Frequencies | Time Complexity: O(n), where n is the length of the array because we iterate through the array once. |
| Sorting and Counting Consecutive Elements | Time Complexity: O(n log n) due to sorting, where n is the number of elements. |
| Hash Table | — |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| HashMap Frequency Counting | O(n) | O(k) | General case when you want the fastest linear scan solution |
| Sorting + Consecutive Counting | O(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 Python/Java solution
How to solve Most Frequent Even Element in O(n)?
What is the best approach for Most Frequent Even Element?
Is Most Frequent Even Element asked at Google/Amazon/Meta?
What data structure is used in Most Frequent Even Element?
What is the time complexity of Most Frequent Even Element?
Ready to solve this problem?
Practice Most Frequent Even Element with our built-in code editor and test cases.
Practice on FleetCodeTable of Contents
Practice this problem
Open in Editor