Created
May 26, 2016 05:59
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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