Highest Frequency

#include <bits/stdc++.h>
using namespace std;

int main()
{
int n;
cin >> n;
vector v;
map<int, int> m;
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
v.push_back(x);
}

for (int i = 0; i < n; i++)
{
    if (m.count(v[i]) == 0)
    {
        m.insert(make_pair(v[i], 1));
    }
    else
    {
        m[v[i]] += 1;
    }
}
int ans = 0;
int maximum = INT_MIN;
for (auto it : m)
{
    if (maximum < it.second)
    {
        maximum = it.second;
        ans = it.first;
    }
}
cout << ans << endl;

}

This code is showing TLE how can i improve it??

use unordered map, array instead of map, vector.
Corrected code

Why was it not working with vector??

and also when we do cin>>x and then assign it to the vector element as v[i]=x it shows TLE why is it so?

the way vector works is that if the vector becomes full it assigns 2 times the current memory and copies the entire vector to new memory, since n is quite large this process would happen more and would be more expensive, so it results in TLE

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.