Highest Frequency( Hashing )

Test Cases 3 and 4 are giving wrong answers.
where am i going wrong–

#include
#include<unordered_map>
using namespace std;
int main() {
unordered_map<int,int>m;
int n,num;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>num;
m[num]++;
}
int maxcount=m[num];
int max=num;
for(auto it=m.begin();it!=m.end();it++)
{
if(it->second>maxcount){
maxcount=it->second;
max=it->first;
}
}
cout<<max;
return 0;
}

Hey Pratyush, if there are many numbers having same frequency then you are supposed to print the number occurring first in the array. For eg.
input:
4
1
2
1
2

your code’s output:
2

but the expected output:
1

Hey Pratyush, as you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.

Please mark your doubts as resolved in your course’s “Ask Doubt” section, when your doubt is resolved.