Given an integer array nums, return the most frequent even element. If there is a tie, return the smallest one. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Given an integer array nums, return the most frequent even element. If there is a tie, return the smallest one.

Hey brothers, please check my code and tell me what's wrong with my codes!! 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. Input: nums = [4,4,4,9,2,4] Output: 4 Explanation: 4 is the even element appears the most. And return -1 if there's no any frequent even! https://code.sololearn.com/cZQn85LY0PCt/?ref=app https://code.sololearn.com/cZQn85LY0PCt/?ref=app

17th Sep 2022, 9:59 AM
follow ->
follow -> - avatar
3 Answers
+ 1
Loop till n-1 only.. for(int i=0;i<n-1;i++){ line:12 as you assigning na[I] = 0 till n-1 only.
17th Sep 2022, 10:14 AM
Jayakrishna 🇮🇳
+ 1
since i goes from 0 to n-1 so there should be n-1 elements only ... The last value will be uninitialized(garbage value)... NOTE: Logic for Tie is missing https://code.sololearn.com/cZHOfijgbr88/?ref=app
17th Sep 2022, 10:17 AM
Prashanth Kumar
Prashanth Kumar - avatar
+ 1
BLACKPINK - 'kill this love ' M/V 01:43 ━━━━●───── 04:40 ⇆ㅤ ㅤ◁ㅤ ❚❚ ㅤ▷ ㅤㅤ↻ ılıılıılıılıılıılı ᴠᴏʟᴜᴍᴇ : ▮▮▮▮▮▮▮▮▮▮
18th Sep 2022, 7:25 AM
Menka
Menka - avatar