# 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; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up