算法一:
using System;
using System.Collections.Generic;
using System.Text;
class Program
...{
private static SortedDictionary<char, int> sd = new SortedDictionary<char, int>();
static void Main(string[] args)
...{
string str = "1232143546534135164161";
foreach(char c in str)
...{
Check(c);
}
char maxChar = Char.MinValue;
int maxValue = 0;
foreach(KeyValuePair<char, int> kvp in sd)
...{
Console.WriteLine("Dictionary has a Key : {0} and Value: {1}", kvp.Key, kvp.Value);
if(kvp.Value > maxValue)
...{
maxValue = kvp.Value;
maxChar = kvp.Key;
}
}
Console.WriteLine("Max char is {0}, times is {1}.", maxChar, maxValue);
Console.ReadLine();
}
private static void Check(char c)
...{
if(sd.ContainsKey(c))//有了
...{
sd[c]++;
}
else
...{
sd.Add(c, 1);
}
}
}
using System.Collections.Generic;
using System.Text;
class Program
...{
private static SortedDictionary<char, int> sd = new SortedDictionary<char, int>();
static void Main(string[] args)
...{
string str = "1232143546534135164161";
foreach(char c in str)
...{
Check(c);
}
char maxChar = Char.MinValue;
int maxValue = 0;
foreach(KeyValuePair<char, int> kvp in sd)
...{
Console.WriteLine("Dictionary has a Key : {0} and Value: {1}", kvp.Key, kvp.Value);
if(kvp.Value > maxValue)
...{
maxValue = kvp.Value;
maxChar = kvp.Key;
}
}
Console.WriteLine("Max char is {0}, times is {1}.", maxChar, maxValue);
Console.ReadLine();
}
private static void Check(char c)
...{
if(sd.ContainsKey(c))//有了
...{
sd[c]++;
}
else
...{
sd.Add(c, 1);
}
}
}
算法2
List<char> list = new List<char>();
string s = "ghjajikdnkxxxxfvnjxxzkxxxnfjkdmfn";
list.AddRange(s.ToCharArray());
list.Sort();
char c=' ';
int temp = 1;
char maxChar=' ';
int maxCount=0;
for (int i = 0; i < list.Count; i++)
...{
if (i > 0)
...{
if (list[i] == c)
...{
temp++;
}
else
...{
temp = 1;
}
}
if (temp > maxCount)
...{
maxCount = temp;
maxChar = list[i];
}
c = list[i];
}
Console.WriteLine(maxChar.ToString()+" "+maxCount.ToString());
string s = "ghjajikdnkxxxxfvnjxxzkxxxnfjkdmfn";
list.AddRange(s.ToCharArray());
list.Sort();
char c=' ';
int temp = 1;
char maxChar=' ';
int maxCount=0;
for (int i = 0; i < list.Count; i++)
...{
if (i > 0)
...{
if (list[i] == c)
...{
temp++;
}
else
...{
temp = 1;
}
}
if (temp > maxCount)
...{
maxCount = temp;
maxChar = list[i];
}
c = list[i];
}
Console.WriteLine(maxChar.ToString()+" "+maxCount.ToString());
算法3
string ss = "fdsafjkdlsajifnalkdnaslkf";
char[] c = ss.ToCharArray();
ArrayList al1 = new ArrayList();
ArrayList al2 = new ArrayList();
int i = 0;
int k = 0;
foreach ( char cc in c)
...{
if (!al1.Contains(cc))
...{
al1.Add(cc);
al2.Add(1);
}
else
...{
al2.Insert(al1.IndexOf(cc),Convert.ToInt32(al2[al1.IndexOf(cc)].ToString()) + 1);
al2.RemoveAt(al1.IndexOf(cc) + 1);
}
}
for ( int j=0;j<al2.Count;j++ )
...{
if ( Convert.ToInt32(al2[j].ToString()) > i)
...{
i = Convert.ToInt32(al2[j].ToString());
k = j;
}
}
string s = "字符" + al1[k].ToString() + "出现" + i.ToString() + "次";
char[] c = ss.ToCharArray();
ArrayList al1 = new ArrayList();
ArrayList al2 = new ArrayList();
int i = 0;
int k = 0;
foreach ( char cc in c)
...{
if (!al1.Contains(cc))
...{
al1.Add(cc);
al2.Add(1);
}
else
...{
al2.Insert(al1.IndexOf(cc),Convert.ToInt32(al2[al1.IndexOf(cc)].ToString()) + 1);
al2.RemoveAt(al1.IndexOf(cc) + 1);
}
}
for ( int j=0;j<al2.Count;j++ )
...{
if ( Convert.ToInt32(al2[j].ToString()) > i)
...{
i = Convert.ToInt32(al2[j].ToString());
k = j;
}
}
string s = "字符" + al1[k].ToString() + "出现" + i.ToString() + "次";
算法4
string s = "skjfklsaghtqw rmq vwec ruwuiey vbre wur q";
StringBuilder sb = new StringBuilder(s);
List<int> charCount = new List<int>();
List<char> lchar = new List<char>();
int i, j;
i = 0; j = 1;
while (sb.Length > 0 && i<sb.Length)
...{
charCount.Add(1);
lchar.Add(sb[0]);
while (sb.Length > 0 && j<sb.Length)
...{
if (sb[0] == sb[j])
charCount[i]++;
j++;
}
sb.Replace(sb[0].ToString(), "");
i++;
j = 1;
}
for (int k = 0; k < charCount.ToArray().Length; k++)
...{
Console.WriteLine(string.Format("{0,4} {1}", lchar[k].ToString(), charCount[k]));
}
Console.ReadLine();
StringBuilder sb = new StringBuilder(s);
List<int> charCount = new List<int>();
List<char> lchar = new List<char>();
int i, j;
i = 0; j = 1;
while (sb.Length > 0 && i<sb.Length)
...{
charCount.Add(1);
lchar.Add(sb[0]);
while (sb.Length > 0 && j<sb.Length)
...{
if (sb[0] == sb[j])
charCount[i]++;
j++;
}
sb.Replace(sb[0].ToString(), "");
i++;
j = 1;
}
for (int k = 0; k < charCount.ToArray().Length; k++)
...{
Console.WriteLine(string.Format("{0,4} {1}", lchar[k].ToString(), charCount[k]));
}
Console.ReadLine();