#include<bits/stdc++.h>
using namespace std;
int countOccurence(int arr[], int n, int k) {
unordered_map<int,int> mp;
for(int i=0;i<n;i++)
{
if(mp.find(arr[i])!=mp.end())
{
mp[arr[i]]++;
}
else if(mp.size()<k-1)
{
mp.insert({arr[i],1});
}
else{
for(auto &i:mp)
i.second--;
for(auto &i:mp)
if(i.second<1) // debugger stops here<--
mp.erase(i.first);
}
}
int res=0;
for(auto i:mp)
{ int count=0;
for(int j=0;j<n;j++)
{
if(arr[j]==i.first)
count++;
}
if(count>n/k)
res++;
}
return res;
}
int main()
{
int arr[] = {54 ,214 ,548 ,54 ,32 ,34 ,32};
cout<<countOccurence(arr,7,4);
}
I was solving a problelm on Count More than n/k Occurences(https://practice.geeksforgeeks.org/problems/count-element-occurences/1).
The code which i have written is the extension of Moore's Voting algorithm.
Actually it passed 32 test cases and on 33rd it gave wrong answer i.e, 2 which was needed to be 3.
And i tried to debug the code but after certain lines debugger suddenly stopped and didn't proceed further.
So i was wondering why did this happen?
This was the situation in the image.