Most Frequent Even Element

Here’s a Python solution that doesn’t rely on any built-in functions like Counter or min:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution:
    def mostFrequentEven(self, nums: List[int]) -> int:
        freq = [0] * 100001
        max_count = -1
        min_val = float('inf')
        for num in nums:
            if num % 2 == 0:
                freq[num] += 1
                if freq[num] > max_count or (freq[num] == max_count and num < min_val):
                    max_count = freq[num]
                    min_val = num
        return min_val if max_count != -1 else -1

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 0-indexed, 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 out-of-bounds errors.

Here is a Python solution using builtin functions:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
from collections import Counter

class Solution:
    def mostFrequentEven(self, nums: List[int]) -> int:
        # Filter the list to include only even numbers
        even_nums = [num for num in nums if num % 2 == 0]

        # If the list is empty after filtering, return -1
        if not even_nums:
            return -1

        # Count the occurrence of each even number
        count = Counter(even_nums)

        # Find the maximum frequency
        max_freq = max(count.values())

        # Get the numbers with the maximum frequency
        max_freq_nums = [num for num, freq in count.items() if freq == max_freq]

        # Return the smallest among them
        return min(max_freq_nums)

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.