linq - How to find maximum number of repeated string in a string in a list of string in c# -


if have list of strings, how can find list of strings have maximum number of repeated symbol using 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  list of s1 + s2 since have **8** repeated '%' 

i tried

 var longest = mylist.where(s => s.length == mylist.max(m => m.length)) ; 

this gives me 1 string not both

here's simple solution, not efficient. every element has count operation performed twice...

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

it return 2 strings:

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

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


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -