如何在c#中的字符串列表中找到字符串中重复字符串的最大数目

本文关键字:字符串 最大数 列表 | 更新日期: 2023-09-27 18:03:23

如果我们有一个字符串列表,那么我们如何使用LINQ找到具有最大重复符号数的字符串列表。

       List <string> mylist=new List <string>();
        mylist.Add("%1");
        mylist.Add("%136%250%3"); //s0
        mylist.Add("%1%5%20%1%10%50%8%3"); // s1
        mylist.Add("%4%255%20%1%14%50%8%4"); // s2
        string symbol="%";
        List <string>  List_has_MAX_num_of_symbol=  mylist.OrderByDescending(s => s.Length ==max_num_of(symbol)).ToList();
//the result should be  a list of s1 + s2 since they have **8** repeated '%'

我试过

 var longest = mylist.Where(s => s.Length == mylist.Max(m => m.Length)) ;

这只给了我一个字符串,而不是两个

如何在c#中的字符串列表中找到字符串中重复字符串的最大数目

这里有一个非常简单的解决方案,但并不完全有效。每个元素都执行了两次Count操作。。。

        List<string> mylist = new List<string>();
        mylist.Add("%1");
        mylist.Add("%136%250%3"); //s0
        mylist.Add("%1%5%20%1%10%50%8%3"); // s1
        mylist.Add("%4%255%20%1%14%50%8%4"); // s2
        char symbol = '%';
        var maxRepeat = mylist.Max(item => item.Count(c => c == symbol));
        var longest = mylist.Where(item => item.Count(c => c == symbol) == maxRepeat);

它将返回2个字符串:

"%1%5%20%1%10%50%8%3">

"%4%255%20%1%14%50%8%4">

这里有一个实现,它依赖于SortedDictionary<,>来获得您想要的东西。

var mylist = new List<string> {"%1", "%136%250%3", "%1%5%20%1%10%50%8%3", "%4%255%20%1%14%50%8%4"};
var mappedValues = new SortedDictionary<int, IList<string>>();
mylist.ForEach(str =>
{
    var count = str.Count(c => c == '%');
    if (mappedValues.ContainsKey(count))
    {
        mappedValues[count].Add(str);
    }
    else
    {
        mappedValues[count] = new List<string> { str };
    }
});
// output to validate output
foreach (var str in mappedValues.Last().Value)
{
    Console.WriteLine(str);
}

这里有一个使用LINQ的程序,它可以得到您想要的结果。

var result = (from str in mylist
    group str by str.Count(c => c == '%')
    into g
    let max = (from gKey in g select g.Key).Max()
    select new
    {
        Count = max,
        List = (from str2 in g select str2)
    }).LastOrDefault();

好的,这是我的答案:

char symbol = '%';
var recs = mylist.Select(s => new { Str = s, Count = s.Count(c => c == symbol) });
var maxCount = recs.Max(x => x.Count);
var longest = recs.Where(x => x.Count == maxCount).Select(x => x.Str).ToList();

它很复杂,因为它有三行(不包括char symbol = '%';行(,但它只对每个字符串计数一次。EZI的答案只有两行,但它很复杂,因为它对每个字符串计数两次。如果你真的想要一个班轮,这里是:

var longest = mylist.Where(x => x.Count(c => c == symbol) == mylist.Max(y => y.Count(c => c == symbol))).ToList();

但它会对每个字符串进行多次计数。你可以选择任何你想要的复杂度。

我们不能假设%总是列表中重复次数最多的字符。首先,我们必须确定每个字符串的单个字符串中出现最多的字符。

一旦我们有了字符及其最大出现次数,我们就可以将Linq应用于List<string>,并获取包含等于其最大出现次数的字符的字符串。

using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
    public static void Main()
    {
        List <string> mylist=new List <string>();
        mylist.Add("%1");
        mylist.Add("%136%250%3");            
        mylist.Add("%1%5%20%1%10%50%8%3");   
        mylist.Add("%4%255%20%1%14%50%8%4"); 
        // Determine what character appears most in a single string in the list
        char maxCharacter = ' ';
        int maxCount = 0;
        foreach (string item in mylist)
        {
            // Get the max occurrence of each character
            int max = item.Max(m => item.Count(c => c == m));
            if (max > maxCount)
            {
                maxCount = max;
                // Store the character whose occurrence equals the max
                maxCharacter = item.Select(c => c).Where(c => item.Count(i => i == c) == max).First();
            }
        }
        // Print the strings containing the max character
        mylist.Where(item => item.Count(c => c == maxCharacter) == maxCount)
            .ToList().ForEach(Console.WriteLine);
    }
}

结果:

%1%5%20%1%10%50%8%3
%4%255%20%1%14%50%8%4

Fiddle演示

var newList = myList.maxBy(x=>x.Count(y=>y.Equals('%'))).ToList();

这应该行得通。如果任何地方有错误,请更正语法,如果对您有用,也请在此处更新。