Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 <= 20000 <= nums[i] <= 105Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[0,1,2,2,4,4,1]
[4,4,4,9,2,4]
[29,47,21,41,13,37,25,7]
majority-element)majority-element-ii)top-k-frequent-elements)sort-characters-by-frequency)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2404: Most Frequent Even Element
class Solution {
public int mostFrequentEven(int[] nums) {
Map<Integer, Integer> cnt = new HashMap<>();
for (int x : nums) {
if (x % 2 == 0) {
cnt.merge(x, 1, Integer::sum);
}
}
int ans = -1, mx = 0;
for (var e : cnt.entrySet()) {
int x = e.getKey(), v = e.getValue();
if (mx < v || (mx == v && ans > x)) {
ans = x;
mx = v;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2404: Most Frequent Even Element
func mostFrequentEven(nums []int) int {
cnt := map[int]int{}
for _, x := range nums {
if x%2 == 0 {
cnt[x]++
}
}
ans, mx := -1, 0
for x, v := range cnt {
if mx < v || (mx == v && x < ans) {
ans, mx = x, v
}
}
return ans
}
# Accepted solution for LeetCode #2404: Most Frequent Even Element
class Solution:
def mostFrequentEven(self, nums: List[int]) -> int:
cnt = Counter(x for x in nums if x % 2 == 0)
ans, mx = -1, 0
for x, v in cnt.items():
if v > mx or (v == mx and ans > x):
ans, mx = x, v
return ans
// Accepted solution for LeetCode #2404: Most Frequent Even Element
use std::collections::HashMap;
impl Solution {
pub fn most_frequent_even(nums: Vec<i32>) -> i32 {
let mut cnt = HashMap::new();
for &x in nums.iter() {
if x % 2 == 0 {
*cnt.entry(x).or_insert(0) += 1;
}
}
let mut ans = -1;
let mut mx = 0;
for (&x, &v) in cnt.iter() {
if mx < v || (mx == v && ans > x) {
ans = x;
mx = v;
}
}
ans
}
}
// Accepted solution for LeetCode #2404: Most Frequent Even Element
function mostFrequentEven(nums: number[]): number {
const cnt: Map<number, number> = new Map();
for (const x of nums) {
if (x % 2 === 0) {
cnt.set(x, (cnt.get(x) ?? 0) + 1);
}
}
let ans = -1;
let mx = 0;
for (const [x, v] of cnt) {
if (mx < v || (mx === v && ans > x)) {
ans = x;
mx = v;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.