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: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);
}
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
Source: stackoverflow.com
Related Query
- Get top 3 most occuring numbers in a List
- How to get top N elements of a list with LinQ?
- C# - Get most common string in a Model list
- Is there a way, using LINQ/EF, to get the top most item in a parent/child hierarchy?
- Get the next number in a list of numbers using LINQ
- Get top 3 most popular items c# linq EF
- Get Top 10 most sold products between date
- How to get certain SQL query results at the top of the list
- Linq to get List with most elements and biggest diff inside it's elements from a List<List<int>>
- How to get top most parent in self reference object
- get the most recent updated record for each ID in the given list of IDs. LINQ
- I can't get pagination to show up in every page on a GridView with a list as source
- Get Highest Average for numbers in a List
- using LINQ to get the top N percent of a list
- How to get the List of products name whose name starts with numbers using LINQ query.?
- Remove duplicate items from list if certain properties match and get the top item based on some ordering
- Linq code to get the index of an object in an array from an object within a list
- How to get top 4 result from a list using linq c#
- List or Array of String Contain specific word in Html Source Code
- Linq Query a list of Objects in VB.NET to get the biggest difference between two numbers
- c# Linq or code to extract groups from a single list of source data
- Query a dictionary to get top 3 most common elements
- Get a list made up of a list of grouped items from a source list
- Get most common values/pattern from a list of lists of objects
- How can I get the index of an item in a list in a single step?
- Get a list of distinct values in List
- How to get duplicate items from a list using LINQ?
- Check if list contains element that contains a string and get that element
- How do I get a distinct, ordered list of names from a DataTable using LINQ?
- Use LINQ to move item to top of list
More Query from same tag
- How can I pass individual values from a string[] to the XDocument.Load Stream?
- LINQ match two lists within a select statement
- How to perform a check on a field condition in a LINQ query?
- Selecting properties of collection items and grouping them by another property
- Check a condition in list except for last entry using linq c#
- Lambda expression C# Union Where
- Hide some object fields before outputing?
- Nullable object must have a value in Linq where clause
- Casting IEnumerable to IList returns null
- MVC3 EF issues on multiple table
- LINQ join on two or more conditions
- Trying to generate all sequences of specified numbers up to a max sum
- Performance of First() vs Last() on a SortedDictionary
- Compare two DataTables with several keys and select the rows that are not present in second table
- [C#]How to introduce retry logic into LINQ to SQL to deal with timeouts?
- Repository Pattern - MVC storefront
- How to rank a list with original order in c#
- How to use LINQ to CRM to populate Entity Readonly Fields (ModifiedOn, Createdby, etc)?
- Put LINQ Query in Method with Property as Parameter
- Add an additional value from LINQ
- Month and Year drop down list for ASP.NET mvc
- Are these improper ways of using LINQ?
- Generate dynamic UPDATE command from Expression<Func<T, T>>
- Using LINQ Contains versus SqlMethods.Like
- Challenge: LINQ to Convert Hierarchy XML to Linked Objects
- How to check for the absence of any value in a list from a set server-side in LINQ? (SQL given)
- Linq C# Join tables if value of two fields exists in another table
- How to average all items in a dictionary and average by key
- Performing operations on a collection of string arrays
- Sort the two date columns individually and cumulative effect?