shlogg · Early preview
Ananya Sinha @ghost_sin109

Counting Distinct Integers In An Array With Frequency Analysis

Counting distinct integers in an array: use a frequency array (fr) to keep track of occurrences. If a value is new, increment counter and set fr[a[i]] to 1. Update fr[a[i]] for each occurrence.

#include<iostream>
using namespace std;
const int Nmax=100005;
int n, fr[Nmax], a[Nmax];
int main(){
        cin>>n;
        int cnt=0;
        for(int i=1;i<=n;i++){
                cin>>a[i];
                if(fr[a[i]]==0){
                        cnt++;
                }
                fr[a[i]]++;
        }
        cout<<cnt<<endl;
        return 0;
}
// For calc the no. of distinct integers in an array.
~                                                                                                             ~...