输入一排字符串,然后统计字符串中出现最多的字符

static void Main(string[] args)
{
string str = "abcabcqwe";

if (str.Length > 0)
{
//把字符串进行分割
char[] strChar = str.ToCharArray();
//把字符先分组后排序
var c = strChar.GroupBy(m => m).OrderByDescending(m => m.Count()).ToList();
var first = strChar.GroupBy(m => m).OrderByDescending(m => m.Count()).First().ToList();//第一位
var last = strChar.GroupBy(m => m).OrderByDescending(m => m.Count()).Last().ToList();//最后一位

List<char> charList = new List<char>();
for (int i = 0; i < c.Count; i++)
{
//得到可能相同的字符
if (first.Count == c[i].Count())
{
charList.Add(c[i].Key);
}
}
//显示字符
string charItem = "";
foreach (var item in charList)
{
charItem += item + ",";
}

Console.WriteLine("{0}是出现最多的字符串 共:{1}次!", charItem, first.Count.ToString());
}
Console.ReadLine();
}

这只是一种做法

时间: 2024-08-05 15:21:02

输入一排字符串,然后统计字符串中出现最多的字符的相关文章

输入一个字符串,查找出出现次数最多的字符

.输入一个字符串,查找出出现次数最多的字符 1 public static void main(String[] args) { 2 // TODO Auto-generated method stub 3 4 System.out.println("请输入一个字符串:"); 5 String str = new Scanner(System.in).next(); 6 int[] a = new int[127]; 7 char maxChar = 0; 8 int max = 0;

数组去重及排序/0~10随机数字/字符串中出现最多的字符及次数

数组去重及排序: var arr = [1,5,1,2,6,8,1,81,9,0]; for(var i=0;i<arr.length;i++){ for(var j=i+1;j<arr.length;j++){ if(arr[i] == arr[j]){ arr.splice(j,1); j--; } } } arr.sort(function(a,b){ return a-b; // 从小到大排序 }) alert(arr); // 0,1,2,5,6,8,9,81 arr.sort(fu

获取在一个数组中出现最多的字符及其所在的位置

获取在一个数组中出现最多的字符.个数及其所在的位置 <!DOCTYPE html> <html> <head> <title>一个数组中,出现次数最多的字符,及其位置</title> </head> <body> <script type="text/javascript"> var arr=["a","x","b","d

js--找字符串中出现最多的字符

在一个字符串中,如 'zhaochucichuzuiduodezifu',我们要找出出现最多的字符.本文章将详细说明方法思路. 先介绍两个string对象中的两个方法:indexOf()和charAt()方法 indexOf()方法介绍 返回某个指定的字符串值在字符串中首次出现的位置 语法 Object(string|array).indexOf(searchValue, fromIndex); 参数1  (必需)规定需检索的字符串值 参数2 (可选)规定在字符串中开始检索的位置(包括自身).若

字符串问题之 字符串的统计字符串

问题1.给定一个字符串str, 返回str的统计字符串, 例如: "aaabbadddffc"的统计字符串为"a_3_b_2_a_1_d_3_f_2_c_1" 进阶题目: 给定一个统计字符串cstr, 在给定一个整数inde  返回代表的字符 问题2.例如: "a_1_b_100"  index=50的字符是'b' 问题1思路: str为空,则统计字符串不存在 str不为空,首先生成String类型res,表示统计字符串,num表示数量(初始位置

20150912华为机考1之&quot;输入一个字符串,将其中出现次数最多的字符输出&quot;

不吐槽华为的服务器了,直接上正文 输入:字符串(英文字母),长度不超过128 输出:出现频率最高的字母 思路写在注释文档 /* Input a string * Output the most frequent character * * The way of thinking: * using ASCII, count the number of each character * then find out the max number(max_num) * and its according

字符串中包含最多的字符

#include<string> #include<iostream> #include<sstream> using namespace std; int main() { string in; cin >> in; int len = in.length(); int temp[26] = { 0 }; for (int i = 0; i < len; i++) { int t = in[i] - 'a'; temp[t]++; } int res

[java] 找出字符串中出现最多的字符和出现的次数

逛园子看到一童鞋做的华为上机题目,写来好长的代码,懒得看,感觉不可能这么难,于是动手敲了下. import java.util.Scanner; public class StringTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); String A = s

查找字符串中出现最多的字符和个数

var str = 'safsafsagadsfshjsadfhsajdhgsaaaaaaaaaaaafsafasdvgdasfsajkodhsaodsagdusiagdusaidhsajdhsajdhsajdhsaohdiusahduisahdusadhudhufsaaaaaagfdghdfhdfhdfhdfgjgfjfgjfddfhfghhdfhdfhdfhdsfgfdghdfhdghfgfdghdfgdfgdfgdsdfgsdfgsgdfgsdgsdfdsfg'; var obj = {}