Highest frequency(TLE problem)

#include
#include<unordered_map>
using namespace std;
int main() {

int n;
cin>>n;
unordered_map<int,int>mp;
int mx=0;
	int y,r=0;
for(int i=0;i<n;i++)
{
	cin>>y;
	if(mp.count(y)!=0)
	{
		mp[y]++;
		if(mp[y]>mx)
		{
			r=y;
		mx=max(mx,mp[y]);
		}
	}
	else
	{
		mp[y]++;
	}
}
cout<<r;
return 0;

}
two test cases are fail

@Pritamkumarcoder, since n can be as large as 10^7 so use scanf and print instead of cin and cout as scanf and print are faster for taking input , or if you want to use cin and cout then you can add these two lines below your in main() function
ios_base::sync_with_stdio(false);
cin.tie(NULL);

also in your code check for max frequency after if else condition , refer code below for better understanding

corrected code :-
https://ide.codingblocks.com/s/281696 (using scanf and printf)
https://ide.codingblocks.com/s/281693 (using cout and cin)

In case of any doubt feel free to ask :slight_smile:
Mark your doubt as RESOLVED if you got the answer

To read more about fast input/output refer this :-

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.