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
- I am getting null data grid, when i bind with datatable in wpf application
- Comparing Two models shared data types
- OrderBy list having different type of values
- Is it necessary to use joins in Linq to Sql
- LINQ for RANK() and CASE WHEN in VB.NET
- Using LINQ to extract ints from a list of strings
- Linq query to find super set with checks
- LINQ Lazy load or query incorrect
- LINQ to SQL insert into database from html form
- Using LINQ to SQL with Dynamic tables
- Linq left join adding data to left object
- Linq int to string
- The "GenerateResource" task failed unexpectedly
- How to search string array's elements in Where Clause ?
- Why is my Linq Oracle DB query not comparing timestamp with datetime correctly?
- linq where contains ALL items in list
- Using IEnumerable methods, perform operation on first and second element of list, then second and third, etc, returning result as new IEnumerable
- Linq to return a value based on another value in an object
- C# - Linq statement where skip last X rows
- c# LINQ joining two lists
- Bizarre Object Null Reference Exception
- Efficient query involving count in subquery
- LINQ query to conditionally filter on a where clause and select the values from another table
- What is the easiest way to save a LINQ query for later use?
- Better way to copy from list to list?
- How to Group by the sum of another Group by?
- Linq statement only executing once and returning zero on second try and above in foreach loop
- How to aggregate delegates into a new delegate with linq
- Find a Non-Duplicate Row in DataTable
- Left Join in Linq with nullable references