---
# System prepended metadata

title: 2404. Most Frequent Even Element
tags: [Leetcode, Easy]

---

# 2404. Most Frequent Even Element
###### tags: `Leetcode` `Easy`
Link: https://leetcode.com/problems/most-frequent-even-element/
## Code
```java=
class Solution {
    public int mostFrequentEven(int[] nums) {
        Map<Integer, Integer> cnt = new HashMap<>();
        int ans = Integer.MAX_VALUE, maxFreq = 0;
        for(int num:nums){
            if(num%2==0){
                cnt.put(num, cnt.getOrDefault(num, 0)+1);
                if(maxFreq<cnt.get(num)){
                    maxFreq = cnt.get(num);
                    ans = num;
                }
                else if(maxFreq==cnt.get(num) && ans>num) ans = num;
            }
        }
        if(ans==Integer.MAX_VALUE) return -1;
        else return ans;
    }
}
```