question

TZacks-2728 avatar image
0 Votes"
TZacks-2728 asked BonnieDeWitt-QnA answered

LINQ return max & min occurrence of word from List<string>

 List<string> data = new List<string>();
             data.Add("Hold");
             data.Add("Hold");
             data.Add("Buy");
             data.Add("Buy");
             data.Add("Hold");
    
             var g = data.GroupBy(i => i);
             foreach (var grp in g)
             {
                 Console.WriteLine("{0} {1}", grp.Key, grp.Count());
             }

I have done the job this way but i want to return max & min occurrence of the word from the list of word using LINQ.

what will happen if two word has same number of occurrence....then which word name will be returned?

please share right LINQ query. thanks

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BonnieDeWitt-QnA avatar image
0 Votes"
BonnieDeWitt-QnA answered

Hi @TZacks-2728 ,

If you mean that you only want two words/counts, the one that has the max occurrence and the one that has the min occurrence, then maybe you want something like this:

 var result =
 (
         data.GroupBy(item => item)
         .Select(itemGroup => new { Item = itemGroup.Key, Count = itemGroup.Count() })
         .OrderByDescending(Item => Item.Count).ThenBy(Item => Item.Item)
 ).ToList(); 
    
 Console.WriteLine("{0} {1}\r\n{2} {3}", result.First().Item, result.First().Count, result.Last().Item, result.Last().Count);

Note that I have used OrderByDescending for the Count, so it will show max first, and ThenBy, which will return the Item alphabetically if the Counts are the same.

I hope that this is what you're asking for. =0)


~~Bonnie DeWitt [MVP since 2003]
http://geek-goddess-bonnie.blogspot.com


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered karenpayneoregon edited

This will provide the count

 List<string> list = new List<string>
 {
     "Hold",
     "Hold",
     "Buy",
     "Buy",
     "Hold"
 };
    
 var result = (from item in list
     group item by item into itemGroup
     select $"{itemGroup.Key} - {itemGroup.Count()}");


Then we have

 List<string> list = new List<string>
 {
     "Hold",
     "Hold",
     "Buy",
     "Buy",
     "Hold"
 };
    
 var result = 
 (
     list.GroupBy(item => item).Select(itemGroup => new { Item = itemGroup.Key, Count = itemGroup.Count() })
  ).ToList();
    
 foreach (var item in result)
 {
     Console.WriteLine($"{item.Item} - {item.Count}");
 }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
1 Vote"
TimonYang-MSFT answered TZacks-2728 commented

The default order is related to the order you add to the List.

You can customize the sorting rules when sorting, such as sorting by first letter(based on Karen's code):

             var re = result.OrderBy(da => da.Count).ThenBy(da=>da.Item);

If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

can we return word which has max & min occurrence ? if possible please guide me with sample code.

Thanks

0 Votes 0 ·