Most Frequent Even Element LeetCode Solution

Problem – Most Frequent Even Element LeetCode Solution

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

Most Frequent Even Element LeetCode Solution in C++

class Solution {
public:
    int mostFrequentEven(vector<int>& nums) {
        map<int, int> mp;
        for(auto n: nums) mp[n]++;
        int ans = -1, mx = -1;
        for(auto m: mp){
            if(m.first%2 == 0 && m.second > mx){
                mx = m.second;
                ans = m.first;
            }
        }
        return ans;
    }
};

Most Frequent Even Element LeetCode Solution in Java

class Solution {
    public int mostFrequentEven(int[] nums) {
        
        Map<Integer,Integer> map=new HashMap<>();
        
        int max=-1;
        int res=Integer.MAX_VALUE;
        
        for(int i:nums){
            
           
            if(i%2 == 0){                        //Only even element
            map.put(i,map.getOrDefault(i,0)+1);
            
            
            if(map.get(i)>max){                 //Check if greater than Max Val
            max=Math.max(max,map.get(i));
            res=i;
            }                                   
            else if(map.get(i)==max && res>i){  //Check if equals to Max Val and element is less than current res
            res=i;
            }
            }
        }
        
        return res==Integer.MAX_VALUE? -1: res; 
        
    }
}

Most Frequent Even Element LeetCode Solution in Python

class Solution(object):
    def mostFrequentEven(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nums = [n for n in nums if n % 2 == 0]
        nums.sort()
        return max(nums,key=nums.count) if len(nums) > 0 else -1
Most Frequent Even Element LeetCode Solution Review:

In our experience, we suggest you solve this Most Frequent Even Element LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Most Frequent Even Element LeetCode Solution

Find on Leetcode

Conclusion:

I hope this Most Frequent Even Element LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.