Brett
Brett

Reputation: 12007

Simple LINQ question in C#

I am trying to use LINQ to return the an element which occurs maximum number of times AND the number of times it occurs.

For example: I have an array of strings:

string[] words = { "cherry", "apple", "blueberry", "cherry", "cherry", "blueberry" };

//...
Some LINQ statement here
//...

In this array, the query would return cherry as the maximum occurred element, and 3 as the number of times it occurred. I would also be willing to split them into two queries if that is necessary (i.e., first query to get the cherry, and second to return the count of 3.

Upvotes: 5

Views: 1166

Answers (8)

Snowbear
Snowbear

Reputation: 17274

var topWordGroup = words.GroupBy(word => word).OrderByDescending(group => group.Count()).FirstOrDefault();
// topWordGroup might be a null!
string topWord = topWordGroup.Key;
int topWordCount = topWordGroup.Count;

And in case if we don't like O(N log N):

var topWordGroup = words.GroupBy(word => word).Aggregate((current, acc) => current.Count() < acc.Count() ? acc : current);

Upvotes: 8

Eric Mickelsen
Eric Mickelsen

Reputation: 10377

Here's a very fast O(n) solution in one line(!):

s.GroupBy(x => x).Aggregate((IGrouping<string,string>)null, (x, y) =>  (x != null && y != null && x.Count() >= y.Count()) || y == null ? x : y, x => x);

Or this:

s.GroupBy(x => x).Select(x => new { Key = x.Key, Count = x.Count() }).Aggregate(new { Key = "", Count = 0 }, (x, y) => x.Count >= y.Count ? x : y, x => x);

Upvotes: 0

Eric Mickelsen
Eric Mickelsen

Reputation: 10377

A simpler O(n) solution:

var groups = words.GroupBy(x => x);
var max = groups.Max(x => x.Count());
var top = groups.First(y => y.Count() == max).Key;

Upvotes: 0

jason
jason

Reputation: 241651

The solutions presented so far are O(n log n). Here's an O(n) solution:

var max = words.GroupBy(w => w)
               .Select(g => new { Word = g.Key, Count = g.Count() })
               .MaxBy(g => g.Count);
Console.WriteLine(
    "The most frequent word is {0}, and its frequency is {1}.",
    max.Word,
    max.Count
);

This needs a definition of MaxBy. Here is one:

public static TSource MaxBy<TSource>(
    this IEnumerable<TSource> source,
    Func<TSource, IComparable> projectionToComparable
) {
    using (var e = source.GetEnumerator()) {
        if (!e.MoveNext()) {
            throw new InvalidOperationException("Sequence is empty.");
        }
        TSource max = e.Current;
        IComparable maxProjection = projectionToComparable(e.Current);
        while (e.MoveNext()) {
            IComparable currentProjection = projectionToComparable(e.Current);
            if (currentProjection.CompareTo(maxProjection) > 0) {
                max = e.Current;
                maxProjection = currentProjection;
            }
        }
        return max;                
    }
}

Upvotes: 12

Frederik Gheysels
Frederik Gheysels

Reputation: 56934

string[] words = { "cherry", "apple", "blueberry", "cherry", "cherry", "blueberry" };

var r = words.GroupBy (x => x)
             .OrderByDescending (g => g.Count ())
             .FirstOrDefault ();
Console.WriteLine (String.Format ("The element {0} occurs {1} times.", r.Key, r.Count ()));

Upvotes: 0

spender
spender

Reputation: 120480

string[] words = { "cherry", "apple", "blueberry", "cherry", "cherry", "blueberry" };

var topWordAndCount=words
    .GroupBy(w=>w)
    .OrderByDescending(g=>g.Count())
    .Select(g=>new {Word=g.Key,Count=g.Count()})
    .FirstOrDefault();

//if(topWordAndCount!=null)
//{
//    topWordAndCount.Word
//    topWordAndCount.Count

Upvotes: 1

diceguyd30
diceguyd30

Reputation: 2752

First thing that comes to mind (meaning there is probably a more efficient way)

var item = words.GroupBy(x => x).OrderByDescending(x => x.Count()).First()
//item.Key is "cherry", item.Count() is 3

EDIT: forgot op wanted the name and the count

Upvotes: 4

Related Questions