Hashtable ht = new Hashtable();
string sr = "aaabbcddddd";
char[] cr = sr.ToCharArray();
foreach (char ch in cr)
{
if (ht.ContainsKey(ch))
{
int i = (int)ht[ch];
ht.Remove(ch);
ht.Add(ch, ++i);
}
else
ht.Add(ch, 1);
}
foreach (DictionaryEntry dicEntry in ht)
{
Console.WriteLine("{0}出现{1}次", dicEntry.Key, dicEntry.Value);
}
ArrayList alist = new ArrayList(ht.Values);
alist.Sort();
object o = (object)alist[alist.Count - 1];
foreach (DictionaryEntry dicEntry in ht)
{
if (dicEntry.Value == o)
{
string str = dicEntry.Key.ToString();
Console.WriteLine("出现次数最多的字符:{0},共{1}次", str, o.ToString());
break;
}
}
Console.ReadLine();
|