Most Frequent Even Element
Here’s a Python solution that doesn’t rely on any builtin functions like Counter
or min
:


This solution uses an array freq
to keep track of the frequencies of even numbers. We traverse the nums
array, and for each even number, we increment its count in freq
. If its count is greater than max_count
, or if it’s equal but the number is less than min_val
, we update max_count
and min_val
. If max_count
remains 1, that means there are no even numbers, so we return 1. Otherwise, we return min_val
, which is the smallest among the most frequent even numbers.
The size of the freq
array is determined by the problem constraints. In this problem, it’s stated that the elements in the nums
array can be up to 10^5.
Since arrays in Python are 0indexed, you need an array that goes up to 10^5 (100,000). This is why the freq
array is initialized with a size of 100,001.
It’s crucial to consider problem constraints when determining sizes for data structures like arrays, as it helps in avoiding outofbounds errors.
Here is a Python solution using builtin functions:


In this solution, we first filter the list to include only even numbers. If there are no even numbers, we return 1. Then we use a Counter to count the occurrence of each even number. We find the maximum frequency, and get the even numbers that have this maximum frequency. If there’s a tie, we return the smallest one.
Frequency Counter is a primary building block in this problem. The Frequency Counter is used to count the frequency of each even number appearing in the input list. This information is then used to determine the most frequent even number.
Another building block here is Condition Checking. Condition Checking is employed here to check whether a number is even (i.e., num % 2 == 0
). If the number is even, it is considered for frequency counting; otherwise, it’s ignored.
Additionally, we also use basic Looping to iterate through the nums
list and the freq
array.
Finally, Tracking/Updating of Variables is a building block used to keep track of the most frequent even number (most_freq_even
) and its frequency (most_freq
).
By combining these building blocks  Frequency Counter, Condition Checking, Looping, and Tracking/Updating of Variables  we can solve this problem effectively.
In this problem we apply an “Even Filter”. This is because we only consider even numbers for frequency count and ignore odd numbers. The term “Filter” is an abstract representation of this operation.