Answer the question
In order to leave comments, you need to log in
How can you sort an array by the number of most repeated values?
For example, there is a sorted array array:
1
1
1
2
3
3
4
4
4
4
5
6
6
7
8
So here's how to sort the array by the number of most repeated values:
4
4
4
4
1
1
1
6
6
3
3
8
7
5
2
Answer the question
In order to leave comments, you need to log in
int[] array = {1, 1, 1, 2, 3, 3, 4, 4, 4, 4, 5, 6, 6, 7, 8};
var sortedByFrequency = array
.GroupBy(o => o)
.OrderByDescending(o => o.Count())
.ThenByDescending(o => o.Key)
.SelectMany(o => o)
.ToArray();
Console.WriteLine(string.Join(",",sortedByFrequency.Select(o => o.ToString())));
//4,4,4,4,1,1,1,6,6,3,3,8,7,5,2
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question