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

T.Zacks 3,996 Reputation points
2021-08-19T16:04:54.857+00:00
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

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,236 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bonnie DeWitt 811 Reputation points
    2021-08-20T21:59:08.643+00:00

    Hi @T.Zacks ,

    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

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Karen Payne MVP 35,471 Reputation points
    2021-08-19T17:27:43.377+00:00

    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}");
    }
    
    1 person found this answer helpful.
    0 comments No comments

  2. Timon Yang-MSFT 9,591 Reputation points
    2021-08-20T06:45:50.987+00:00

    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 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.