【问题标题】:How to find maximum number of repeated string in a string in a list of string in c#如何在c#中的字符串列表中查找字符串中重复字符串的最大数量
【发布时间】:2015-10-07 20:05:12
【问题描述】:

如果我们有一个字符串列表,那么我们如何使用 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)) ;

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

【问题讨论】:

  • 你的问题写的不是很清楚。您是否在寻找包含最大重复字符数、最大重复计数或其他内容的字符串?
  • @jdphenix 现在更新了
  • 根据您对我的回答的 cmets,我不得不假设您对问题中所需内容的解释仍然不正确。我投票结束,因为不清楚你在问什么。如果可以,请澄清 - 谢谢。

标签: c# linq computer-science


【解决方案1】:

这是一个依赖于SortedDictionary&lt;,&gt; 的实现来获得你想要的东西。

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

【讨论】:

  • 您期待一行代码还是一个方法调用?那不会发生。
  • 看这段代码给了我第一次出现的机会:- var long = mylist.Where(s => s.Length == mylist.Max(m => m.Length));
  • 您没有要求最长的字符串,您要求的是% 字符数最多的字符串。
  • 还在等更聪明的人:)
  • @JohnKanti 我们也在这里等待聪明的问题。如果你不能得到它,你必须和可用的一起生活,就像你的问题一样。
【解决方案2】:
var newList = myList.maxBy(x=>x.Count(y=>y.Equals('%'))).ToList();

这应该可行。如果在任何地方出现错误,请更正语法,如果它适合您,请在此处更新。

【讨论】:

  • 注意提供拒绝投票的理由,这样我下次就可以避免这个错误。
  • 可能是未知方法maxBy?顺便说一句:OP 想要 2 个结果而不是 maxBy 所暗示的结果。
【解决方案3】:

好的,这是我的答案:

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

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

【讨论】:

    【解决方案4】:

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

    一旦我们有了字符和它的最大出现次数,我们就可以将Linq 应用于List&lt;string&gt; 并获取包含等于其最大出现次数的字符的字符串。

    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 Demo

    【讨论】:

      【解决方案5】:

      这是一个非常简单的解决方案,但效率不高。每个元素都有两次 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”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-17
        • 1970-01-01
        • 1970-01-01
        • 2021-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多