字符串中字母的最多出现 [英] most occurence of letter in string

查看:117
本文介绍了字符串中字母的最多出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个接受字符串并返回字母的函数
在该字符串中出现最多的

I am looking for a function that takes in a string and returns the letter that
appears the most in that string

推荐答案

您可以使用其他方法来实现.

示例1 [示例2 [ ^ ]
You can achieve this with different methods.

Example 1[^]

Example 2[^]


最后一个解决方案将帮助您
The last solution will help you here.


没有标准功能可以执行:您可能可以在Linq中执行此操作,但是此组合也可以使用:
There is no standard function that will do it: you can probably do it in Linq, but this combo also works:
private static char FindMostFrequent(string input)
    {
    char[] distinct = input.Distinct().ToArray();
    int max = -1;
    char most = input[0];
    foreach (char d in distinct)
        {
        int count = input.Count(c => c == d);
        if (count > max)
            {
            max = count;
            most = d;
            }
        }
    return most;
    }


这篇关于字符串中字母的最多出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆