110,533
社区成员
发帖
与我相关
我的任务
分享
var query="bacea".ToCharArray().GroupBy(x=>x)
.OrderByDescending(g=>g.Count())
.Select(g=>new{g.Key,cnt=g.Count()})
.FirstOrDefault();
Console.WriteLine("{0} 出现 {1} 次",query.Key,query.cnt);
//a 出现 2 次
int[] x = new int[26];
char[] ca = str.toCharArray();
for(int i=0;i <ca.length;i++){
x[ca[i]-'a']++;
}
for(int i=0;i <ca.length;i++){
System.out.println("字符"+(char)('a'+i)+"出现了"+ca[i]+"次");
}
public static Hashtable CharSameCount(string Str)
{
char[] TempArray = Str.ToCharArray();
ArrayList nStr = new ArrayList();
for (int i = 0; i < TempArray.Length; i++)
{
if (!nStr.Contains(TempArray[i]))
{
nStr.Add(TempArray[i]);
}
}
char[] newStr = (char[])nStr.ToArray(typeof(char));
Hashtable ht = new Hashtable();
for (int count = 0; count < newStr.Length; count++)
{
string tempStr = Str.Replace(newStr[count].ToString(), "");//在此进行字符串字符替换为空
ht.Add(newStr[count], Str.Length - tempStr.Length);//原长度减替换后的长度
Str = tempStr;
}
return ht;//返回字符串中每个字符个数的哈希表。
}
string x = "bacea";
foreach (DictionaryEntry a in CharSameCount(x))
{
MessageBox.Show("字符:"+a.Key.ToString()+" 数量:"+a.Value.ToString());
}