Frequency value is not updating in my code

I tried to create duplicate array and count a non zero numbers frequency, but frequency value remains one and does not change . Please debug

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

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t,ns,fimax=0,smax=0,hrec=0,brec=0,freq=0;
    cin>>t;
    while(t--){
        cin>>ns;
        int a[ns];int d[ns];
        for(int i=0;i<ns;i++){//to create array and duplicate arrray
            cin>>a[i];
            d[i]=a[i];
        }
        for(int i=0;i<ns;i++){
            if (d[i]==0){continue;}
            for(int j=0;j<ns;j++){//to count frequency and change value in duplicate array
                if(d[i]==d[j]){
                    freq=freq+1;
                    d[j]=0;
                    cout<<"this is value of freq"<<freq<<endl;
                }
            }
        cout<<freq<<" this is value of freq of arry element "<<a[i]<<" of index "<<i<<endl;
        freq=0;
        }


}
}```

Hi.
I haven’t done arrays yet, but I somehow ended up managing to fix your code (taking a lot of time). Just some minor modifications. Here it is (some of the stuff has been changed, but your code is intact, more or less. The only real changes are in:

  1. The if - statement ( if (d[i]==d[j] ), and
  2. The final output statement (To give it a slightly neater look).

The updated code:

Mensan naat :slight_smile:

1 Like

Can you please send the code???
And what was wrong with my previous code???
freq=0 was not placed well i know that but freq value behaves weirdly even if i put freq=0 in proper place

Check this code once:

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t,ns,fimax=0,smax=0,hrec=0,brec=0;
    cin>>t;
    while(t--){
        cin>>ns;
        int freq=0;
        int a[ns];int d[ns];
        for(int i=0;i<ns;i++){//to create array and duplicate arrray
            cin>>a[i];
            d[i]=a[i];
        }
        for(int i=0;i<ns;i++){
            if(d[i]==0 )continue;
            for(int j=0;j<ns;j++){
                if(d[i]==d[j]){
                    freq=freq+1;
                    d[j]=0;
                }
                d[i]=0;
            }
            cout<<"This is frequency"<<freq<<endl;
            freq=0;
             cout<<"check frequency"<<freq<<endl;
        }


}
}