score:1
Accepted answer
This should work fine
var topThreeMostOccuring = arr.GroupBy(x => x)
.OrderByDescending(g => g.Count())
.SelectMany(x => x.Take(1)).Take(3);
score:1
You can use Linq :
var top3 = list.GroupBy(x => x)
.OrderByDescending(x => x.Count())
.Take(3)
.Select(x => x.Key);
Console.WriteLine(string.Join(" ", top3)); // output 5 3 4
score:0
Hope this helps you
var list = new List<int>();
list.Add(5);
list.Add(5);
list.Add(27);
list.Add(4);
list.Add(3);
list.Add(4);
list.Add(4);
list.Add(29);
list.Add(3);
list.Add(5);
list.Add(4);
var TheMostoccurring = list.GroupBy(n => n).OrderByDescending(m => m.Count()).Select(m => m.Key).ToList().Take(3);
foreach (var item in TheMostoccurring)
{
Console.WriteLine(item);
}
Source: stackoverflow.com
Related Articles
- Get top 3 most occuring numbers in a List
- List or Array of String Contain specific word in Html Source Code
- c# Linq or code to extract groups from a single list of source data
- Populate a list with a specific range of numbers by using LINQ
- Linq to Objects - return pairs of numbers from list of numbers
- How to return null if using SingleOrDefault() and searching a list of numbers for a number not in list?
- Generating the Shortest Regex Dynamically from a source List of Strings
- Is it possible to use LINQ to check if all numbers in a list are increasing monotonically?
- C# linq list find closest numbers
- C# - Get most common string in a Model list
- Most performant way to filter a huge List in C#?
- LINQ Source Code Available
- .NET 4 Code Contracts: "requires unproven: source != null"
- How to find how many times the same object appears in a list and then find a property of the most appeared object
- OrderBy numbers and ThenBy boolean not working for List
- LINQ query returns old results when source list is re-initialized
- Copy a list of objects to another typed list in one line of code
- Get the next number in a list of numbers using LINQ
- C# - Linq optimize code with List and Where clause
- Linq select all numbers that are in any interval from another list of intervals
- creating Linq to sqlite dbml from DbLinq source code
- C# order a list of alphanumeric numbers
- How can i copy data table records of different field name based on mapping list evaluating condition on source data table?
- Find the most frequent numbers in an array using LINQ
- C# - How to find most frequent date from list
- Most efficient way to find a "space" in an ordered list using LINQ
- what is the most elegant way to update an item in one list from another list in C#?
- Most efficient way to remove items from a list without running into a collection modified exception?
- Linq to get List with most elements and biggest diff inside it's elements from a List<List<int>>
- C# find most occurring value in a list of strings
- 3 tables linq join plus foreach
- Linq: split list based on property value
- Finding the list of common objects between two lists
- Get element or default value?
- How to use ToString SelectListItem with Entity Framework?
- Linq Ternary in where clause to fall through to all records
- Convert SQL to LINQ query to get max
- adding datarows from array of datarows to top of datatable
- Translating LINQ expression from C# into VB.NET throws exception in Group by
- Linq Total of a Group
- Unique Sub-JSon object of a Json object in C#
- Conditional Select() in Select()
- C# Linq-to-XML on XCCDF
- Filtering by Where clause only when condition is not null
- How to use Linq query Result in OrderBy?
- Inner query inside Wcf Services query throws 'NotSupportedException'
- cosmosdb where clause in sub lists with linq
- Get value and index first low value in array
- filtering collection using linq
- How to Process Lambda Expressions Passed as Argument Into Method - C# .NET 3.5