Most Frequent Even Element

🏠 ⬅️ ➡️

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

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: n = 8
    integer, dimension(n) :: nums
    integer :: i, j, k, l, m, n_even, max_count, min_value
    logical :: found

    ! Examples
    nums(1) = 0
    nums(2) = 1
    nums(3) = 2
    nums(4) = 2
    nums(5) = 4
    nums(6) = 4
    nums(7) = 1
    nums(8) = 4

    ! Solve the problem
    n_even = 0
    max_count = 0
    min_value = 0
    do i = 1, size(nums)
        if (mod(nums(i), 2) == 0) then
            n_even = n_even + 1
            found = .false.
            do j = 1, n_even
                if (nums(i) == nums(j)) then
                    found = .true.
                    exit
                end if
            end do
            if (.not. found) then
                nums(n_even) = nums(i)
            end if
        end if
    end do

    if (n_even > 0) then
        do i = 1, n_even
            k = 0
            do j = 1, n_even
                if (nums(i) == nums(j)) then
                    k = k + 1
                end if
            end do
            if (k > max_count) then
                max_count = k
                min_value = nums(i)
            else if (k == max_count) then
                if (min_value > nums(i)) then
                    min_value = nums(i)
                end if
            end if
        end do
    end if

    write (*, '(I0)') min_value

end program main
Compiled
Executed
Correct
module FrequentEven
    implicit none
    private
    public :: main

contains

    subroutine main
        integer, allocatable :: nums(:)
        integer :: i, n, even_count, even_max_count, even_max_val

        read (*, *) n
        allocate (nums(n))
        do i = 1, n
            read (*, *) nums(i)
        end do

        even_count = 0
        even_max_count = 0
        even_max_val = -1
        do i = 1, n
            if (mod(nums(i), 2) == 0) then
                even_count = even_count + 1
                if (even_count > even_max_count) then
                    even_max_count = even_count
                    even_max_val = nums(i)
                else if (even_count == even_max_count) then
                    if (nums(i) < even_max_val) then
                        even_max_val = nums(i)
                    end if
                end if
            end if
        end do

        if (even_max_val == -1) then
            write (*, *) -1
        else
            write (*, *) even_max_val
        end if

        deallocate (nums)
    end subroutine main
end module FrequentEven
🌐 Data from online sources
def most_frequent_even(nums):
    count = {}
    for num in nums:
        if num % 2 == 0:
            count[num] = count.get(num, 0) + 1
    max_count = float('-inf')
    most_frequent = -1
    for num, freq in count.items():
        if freq > max_count or (freq == max_count and num < most_frequent):
            max_count = freq
            most_frequent = num
    return most_frequent
The algorithm uses a hash map to count the frequency of even numbers in the given array. The hash map is iterated and the even number with the highest frequency is stored as the most frequent. If there is a tie, the smaller number is chosen as the most frequent. If no even number is found, the algorithm returns -1.
🌐 Data from online sources
int mostFrequentEven(vector<int>& nums) {
    unordered_map<int, int> count;
    for (int num : nums) {
        if (num % 2 == 0) {
            count[num]++;
        }
    }
    int max_count = INT_MIN;
    int most_frequent = -1;
    for (auto& [num, freq] : count) {
        if (freq > max_count || (freq == max_count && num < most_frequent)) {
            max_count = freq;
            most_frequent = num;
        }
    }
    return most_frequent;
}
The algorithm uses a hash map to count the frequency of even numbers in the given array. The hash map is iterated and the even number with the highest frequency is stored as the most frequent. If there is a tie, the smaller number is chosen as the most frequent. If no even number is found, the algorithm returns -1.