1.C语言方法
#include <stdio.h>
int main()
{
const char* ps = "hello world";
char c = '\0';
size_t counts[256] = { 0 };
for( const char* p=ps; *p; ++p )
{
++counts[*p];
if( counts[*p] > counts[c] )
c = *p;
}
printf( "Character=%c, count=%zu\n", c, counts[c] );
return 0;
}
2.Hash-Table
#include <iostream>
#include <stdio.h>
#include <map>
using namespace std;
int main()
{
const char* ps = "hello world";
std::map<char, int> tmp;
char c = '\0';
for (const char* p = ps; *p; ++p)
tmp[*p]++;
char max_key;
int max =0;
for (auto pp = tmp.begin(); pp != tmp.end(); pp++)
if (max < pp->second)
{
max = pp->second;
max_key = pp->first;
}
printf("Character=%c, count=%zu\n", max_key, max);
return 0;
}