How to get the top N highest values from array in C#

1 Answer

0 votes
using System;
using System.Linq;
using System.Collections.Generic;
 
class Program
{
    static void Main() {
        int[] arr = { 12, 98, 80, 50, 88, 35, 60, 97, 91, 85, 89 };
 
        int N = 4;
        IEnumerable<int> topThree = arr.OrderByDescending(val => val).Take(N);
 
 
        foreach (int n in topThree) {
            Console.WriteLine(n);
        }
    }
}
  
  
  
  
/*
run:
  
98
97
91
89
  
*/

 



answered Jun 8, 2021 by avibootz

Related questions

1 answer 205 views
1 answer 257 views
1 answer 148 views
1 answer 141 views
1 answer 144 views
1 answer 83 views
1 answer 263 views
...