How to get the top 3 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, 85, 89 };

        IEnumerable<int> topThree = arr.OrderByDescending(val => val).Take(3);


        foreach (int n in topThree) {
            Console.WriteLine(n);
        }
    }
}
 
 
 
 
/*
run:
 
98
97
89
 
*/

 



answered Jun 8, 2021 by avibootz

Related questions

1 answer 257 views
1 answer 118 views
1 answer 205 views
1 answer 141 views
1 answer 144 views
1 answer 173 views
1 answer 191 views
...