如何查找一个字符串中重复次数最多的字符,返回这个字符的个数和字符

解决方案 »

  1.   

    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);
            }
        }
    }
      

  2.   

    我只会建个数组 然后建个Dictionary 然后就挨个数呗
      

  3.   

    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());
      

  4.   

    用split.存在数组里,然后读数组的大小
    顺:www.babykey.com
      

  5.   

    string str = "1232143546534135164161";
    int intMax = 0;
    int intTemp = 0;
    foreach(char c in str)
    {
    intTemp = GetNum(str,c);
    if ( intTemp > intMax )
    {
    intMax = intTemp;
    chrMax = c;
    }
    }
      

  6.   

    private int getNum(string str1,char c)
    {
            string []strTemp;
    strTemp = str1.Split(c);
    return strTemp.Length-1;
    }
      

  7.   

    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() + "次";
      

  8.   

    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();