Max frequency character

please explain me the following code, what is purpose of freq[] array, how has it been used

#include
using namespace std;
int main() {
char str[1000];
int freq[256];
cin>>str;

for(int x=0;str[x]!='\0';x++){
    freq[str[x]]++;
}
int ans=0;
char ch;
for(int x=0;x<256;x++){
    if(freq[x]>0){
        if(freq[x]>ans){
            ans=freq[x];
            ch=char(x);
        }
    }
}
cout<<ch;

}

hello @s.kumar

freq array is holding frequency of each character.

this loop is iterating whole string and then for each character we are increasing its count by one in freq array

here we are simply iterating the frequency array and picking that char whch has maximum frequency.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.