Doubt related to Highest frequency Hashing

question -Given an array find the number which comes with maximum frequency. It must work in O(n) time complexity.

Input Format
Enter the size of the array N and add N more numbers and store in an array

Constraints
Output Format
Display the number with the maximum frequency.

Sample Input
5
1
2
2
2
3

my solution -one test case fail https://ide.codingblocks.com/s/120094

#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
int arr[n];
unordered_map<int,int> map;
for(int i=0; i<n; i++)
{
cin>>arr[i];
map[arr[i]]++;
}
int max=0,res;
for(auto x:map)
{
if(x.second==max)
{
res=x.first;
}
else if(x.second>max)
{
max=x.second;
res=x.first;
}
}
cout<<res;
return 0;
}