三种方式统计string中出现次数最多的char(还是LinQ最简便)

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Collections;

namespace ConsoleApplication1
{

    class Program
    {
        static void Main(string[] args)
        {
            string str = "aaeaabbebbccececddeddd";

            List<char> charList = new List<char>(str.ToCharArray());
            int len = charList.Count;

            Dictionary<char, int> charD = new Dictionary<char, int>();

            int lastTime = 0;

            while (charList.Count != 0)
            {
                int count = CountAChar(charList);

                if (count > lastTime)
                {
                    charD.Clear();
                    charD.Add(charList[0], count);
                    lastTime = count;
                }

                else if (count == lastTime)
                {
                    charD.Add(charList[0], count);
                    lastTime = count;
                }

                DeleteAChar(count, charList);
            }

            foreach (KeyValuePair<char, int> kv in charD)
            {
                Console.WriteLine("{1}有{0}个", kv.Value.ToString(), kv.Key.ToString());
            }

            Console.WriteLine("----------------------------------------");
            SecondProgram.SecondWay(str);
            Console.WriteLine("----------------------------------------");
            ThirdClass.ThirdWay(str);
            Console.Read();
        }

        static void DeleteAChar(int num, List<char> cl)
        {
            char c = cl[0];

            for (int i = 0; i < num; i++)
            {
                cl.Remove(c);
            }
        }

        static int CountAChar(List<char> cl)
        {
            int ret = 0;

            foreach (char c in cl)
            {
                if (c == cl[0])
                {
                    ret++;
                }
            }

            return ret;
        }
    }

    //-----------------------------------------------------------------
    class SecondProgram
    {
        public static void SecondWay(string str)
        {
            int len = str.Length;
            char[] strArray = str.ToCharArray();
            int[] count = new int[len];
            for (int y = 0; y < len; y++) //将count数组的每个单元初始为0
                count[y] = 0;

            for (int y = 0; y < len; y++) //将当前字符与之后的字符进行比较,相同count数组中对应索引处++
            {
                for (int yy = y; yy < len; yy++)
                {
                    if (strArray[y].Equals(strArray[yy]))
                    {
                        count[y]++;
                    }
                }
            }

            ArrayList maxCountArr = new ArrayList();
            int maxCount = count[0];

            for (int y = 0; y < len; y++) //获得出现次数的最大值
            {
                if (count[y] > maxCount)
                {
                    maxCount = count[y];
                }
            }
            for (int y = 0; y < len; y++) //最大值与count数组中的每个数进行比较,相同则出现次数相同,把索引加入到maxCountArr
            {
                if (count[y] == maxCount)
                {
                    maxCountArr.Add(y);
                }
            }
            for (int y = 0; y < maxCountArr.Count; y++) //输出
            {
                Console.Write("Str" + (y + 1) + ":" + strArray[(int)maxCountArr[y]] + "/n");
            }
        }
    }
    /// <summary>
    /// 第三种方式
    /// </summary>
    class ThirdClass
    {
        /// <summary>
        /// 还是LinQ最简便
        /// </summary>
        /// <param name="str"></param>
        public static void ThirdWay(string str)
        {
            var resultGroup = from aChar in str.ToCharArray()
                              group aChar by aChar;

            int max = 0;

            foreach (var one in resultGroup)
            {
                if (one.Count() > 0)
                {
                    max = one.Count();
                }
            }

            foreach (var one in resultGroup)
            {
                if (one.Count() == max)
                {
                    Console.WriteLine("{0}字符出现了{1}次", one.Key, max);
                }
            }

        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值