High Frequency doubt in code

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

int main() {

int t;
cin>>t;

while(t--){
    
    int n;
    cin>>n;
    
    vector<int> arr(n);
    
    for(int i = 0; i < n; i++){
        cin>>arr[i];
    }
    
    vector<int> p((int)1e5+1, 0);
    
    for(auto x: arr)
        p[x]++;
        
    int an1 = -1;

    int an2 = -1;
        
    for(auto x: p){
        
        if(x > an1){
            an1 = x;
        }
        else if(x > an2)
            an2 = x;
        
    }
   
   if(an1%2)
    an1 = an1 / 2 + 1;
    
   else
    an1 = an1/2;
    
   cout<<max(an1, an2)<<"\n";
}
return 0;

}

The code is almost same and could not figure out whats the error in that?

the problem: CodeChef
your code fixed: Solution: 70137167 | CodeChef

Your error:

 for(auto x: p){
    if(x > an1){
        //HERE you should store an1 inside an2
        an1 = x;
    }
    else if(x > an2)
        an2 = x;
}

Here i am finding second maximum and then i will check number of maximum value exist