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

 算法一: 


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


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

 

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

 

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


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值