Difficulty: Easy | Acceptance: 53.60% | Paid: No Topics: Array, Hash Table, Counting
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.
- Examples
- Constraints
- Hash Map Approach
- Sorting Approach
- Array Counting Approach
Examples
Input: nums = [0,1,2,2,4,4,4]
Output: 4
Explanation: The even elements are 0, 2, 2, 4, 4, 4. The most frequent even element is 4.
Input: nums = [4,4,4,9,2,4]
Output: 4
Explanation: The even elements are 4, 4, 4, 2, 4. The most frequent even element is 4.
Input: nums = [29,47,21,41,2,2,2,2,2]
Output: 2
Explanation: The even elements are 2, 2, 2, 2, 2. The most frequent even element is 2.
Constraints
- 1 <= nums.length <= 2000
- 0 <= nums[i] <= 10^5
Hash Map Approach
Intuition Use a hash map to count frequencies of even elements, then find the element with highest frequency (smallest value on tie).
Steps
- Iterate through array and count only even elements using a hash map
- If no even elements found, return -1
- Iterate through hash map entries to find element with maximum frequency
- On tie, keep the smaller element
python
class Solution:
def mostFrequentEven(self, nums: List[int]) -> int:
freq = {}
for num in nums:
if num % 2 == 0:
freq[num] = freq.get(num, 0) + 1
if not freq:
return -1
max_freq = -1
result = -1
for num in sorted(freq.keys()):
if freq[num] > max_freq:
max_freq = freq[num]
result = num
return resultComplexity
- Time: O(n + k log k) where k is number of unique even elements
- Space: O(k) for the hash map
- Notes: Sorting keys adds log k factor, but can be avoided with careful comparison
Sorting Approach
Intuition Sort the array first, then count consecutive occurrences of each even element while tracking the maximum.
Steps
- Sort the array in ascending order
- Iterate through sorted array, counting consecutive identical even elements
- Track the element with highest frequency (smaller values naturally come first)
- Return -1 if no even elements found
python
class Solution:
def mostFrequentEven(self, nums: List[int]) -> int:
nums.sort()
max_freq = 0
result = -1
i = 0
n = len(nums)
while i < n:
if nums[i] % 2 == 0:
j = i
while j < n and nums[j] == nums[i]:
j += 1
freq = j - i
if freq > max_freq:
max_freq = freq
result = nums[i]
i = j
else:
i += 1
return resultComplexity
- Time: O(n log n) due to sorting
- Space: O(1) or O(n) depending on sorting implementation
- Notes: Simpler logic but slower due to sorting overhead
Array Counting Approach
Intuition Since values are bounded (0 to 10⁵), use a fixed-size array for counting instead of hash map for better performance.
Steps
- Create array of size 100001 initialized to 0
- Count occurrences of each even element by incrementing at index
- Iterate through even indices (0, 2, 4, …) to find maximum frequency
- Return -1 if no even elements were counted
python
class Solution:
def mostFrequentEven(self, nums: List[int]) -> int:
MAX_VAL = 100000
freq = [0] * (MAX_VAL + 1)
for num in nums:
if num % 2 == 0:
freq[num] += 1
max_freq = 0
result = -1
for i in range(0, MAX_VAL + 1, 2):
if freq[i] > max_freq:
max_freq = freq[i]
result = i
return resultComplexity
- Time: O(n + MAX_VAL) where MAX_VAL = 10⁵
- Space: O(MAX_VAL) for the counting array
- Notes: Most efficient for this specific constraint, but uses more memory