List<int> a = new List<int>{ 1,1,2,2,3,4,5 };
What's the quickest way to do this with LINQ?
I'm new to LINQ
List<int> a = new List<int>{ 1,1,2,2,3,4,5 };
What's the quickest way to do this with LINQ?
I'm new to LINQ
from num in a
group num by num into numg
let c = numg.Count()
order by c descending
select new { Number = numg.Key, Count = c }
The key here is using Enumerable.GroupBy
and the aggregation method Enumerable.Count
:
List<int> list = new List<int>() { 1,1,2,2,3,4,5 };
// group by value and count frequency
var query = from i in list
group i by i into g
select new {g.Key, Count = g.Count()};
// compute the maximum frequency
int whatsTheFrequencyKenneth = query.Max(g => g.Count);
// find the values with that frequency
IEnumerable<int> modes = query
.Where(g => g.Count == whatsTheFrequencyKenneth)
.Select(g => g.Key);
// dump to console
foreach(var mode in modes) {
Console.WriteLine(mode);
}
Jason's answer is correct, but you can perform this operation in one LINQ operation.
List<int> list = new List<int>() { 1, 1, 2, 2, 3, 4, 5 };
// return most frequently occurring items
var query = from i in list
group i by i into g
let maxFreq = (from i2 in list
group i2 by i2 into g2
orderby g2.Count() descending
select g2.Count()).First()
let gCount = g.Count()
where gCount == maxFreq
select g.Key;
// dump to console
foreach (var mode in query)
{
Console.WriteLine(mode);
}
I think the most frequent number can also be achieved in a single query like this-
var query = (from i in list
group i by i into g
orderby g.Count() descending
select new { Key = g.Key, Count = g.Count() }).FirstOrDefault();
if (query == null) Console.WriteLine("query = NULL");
else Console.WriteLine("The number '{0}' occurs {1} times.", query.Key, query.Count);
Null check is not really required but it may be useful when null is actually expected (like Empty list?)