Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created May 26, 2016 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/d530d47982262de8a5e1cf5b49f7b54a to your computer and use it in GitHub Desktop.
Save jianminchen/d530d47982262de8a5e1cf5b49f7b54a to your computer and use it in GitHub Desktop.
string function - find most frequent char in a string - http://www.cnblogs.com/luxiaoxun/archive/2012/11/12/2766095.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace stringMostFrequentOne
{
class Program
{
static void Main(string[] args)
{
}
/*
* Ascii value is in the range from [-128, 127]
* so, add +128 to fit in the range [0, 255]
*/
public static void get_most(char[] s, ref char ch, ref int maxSize)
{
ch = '\0';
maxSize = 0;
if (null != s)
{
int[] n = new int[256];
//memset(n, 0, sizeof(n));
int count = 0;
while(s[count] != '\0')
{
char c = s[count];
int index = c + 128;
n[index] += 1;
if ((n[index]) > maxSize)
{
maxSize = n[index];
ch = c;
}
count ++;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment