Skip to content

Commit 51d45c8

Browse files
committed
Runtime: 17 ms (Top 70.51%) | Memory: 44.80 MB (Top 50.15%)
1 parent 12cf9bc commit 51d45c8

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Runtime: 17 ms (Top 70.51%) | Memory: 44.80 MB (Top 50.15%)
2+
3+
class Solution {
4+
public int mostFrequentEven(int[] nums) {
5+
6+
HashMap<Integer,Integer>counts =new HashMap<>();
7+
for(int i:nums)
8+
{
9+
if(i%2==0)
10+
counts.put(i,counts.getOrDefault(i,0)+1); }
11+
int ans=-1 ,maxFreq=-1;
12+
13+
for(Integer num:counts.keySet()){
14+
15+
if(counts.get(num)>maxFreq){
16+
maxFreq=counts.get(num);
17+
ans=num;
18+
}
19+
else if(counts.get(num)==maxFreq && ans>num){
20+
ans=num;
21+
}
22+
}
23+
return ans;
24+
}
25+
}

0 commit comments

Comments
 (0)