0
  #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?

enter image description here

This was the situation in the image.

  • 2
    Long run you’ll probably be better off getting tools like this running locally, i.e. on your own computer. If you like GDB, you can get it via WSL on Windows; if you want to try a GUI debugger, Visual Studio Community has an excellent debugger for Windows. Both will offer a better experience than random websites. – nneonneo Mar 29 '23 at 18:36
  • Note `received signal SIGSEGV` at the bottom. Your code crashed, i.e. stopped with an error. – HolyBlackCat Mar 29 '23 at 18:41
  • 2
    You can't modify a container while iterating over it. Erasing an element invalidates the iterators internally stored by ranged-for. You should either use a flavor of loop shown [here](https://en.cppreference.com/w/cpp/container/unordered_map/erase#Example), or [`std::erase_if()`](https://en.cppreference.com/w/cpp/container/unordered_map/erase_if) for a fancy oneline solution. – HolyBlackCat Mar 29 '23 at 18:52
  • Your program crashes with segmentation fault, which means there is a memory issue. I think the issue is that you are erasing elements while iterating the map. – Aristotelis V Mar 29 '23 at 18:53
  • 2
    Also read: [Why should I not #include ?](https://stackoverflow.com/q/31816095/2752075), [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/2752075). Also see [MSYS2](https://stackoverflow.com/q/30069830/2752075) for a way to install GDB locally, if you want to use it for Windows apps (as opposed to GDB on WSL for Linux apps). – HolyBlackCat Mar 29 '23 at 18:54
  • Side note: Be cautious with Geeks for Geeks. It is a poorly curated resource (anyone can post anything with few guardrails) and contains a lot of mistakes and some truly staggeringly bad advice. Some days it feels like half of Stackoverflow's traffic is helping folks unlearn what they read on GfG. – user4581301 Mar 29 '23 at 19:01

0 Answers0