How to find the k biggest values from an array in C#

1 Answer

0 votes
using System;
 
class Program
{
    static int[] PickMaxK(int[] arr, int k)
    {
        Array.Sort(arr);
        Array.Reverse(arr);
    
        int[] result = new int[k];
        Array.Copy(arr, result, k);
    
        return result;
    }

    static void Main()
    {
        int[] arr = { 11, 2, 4, 9, 3, 6, 5, 1 };
        int k = 3;
 
        int[] result = PickMaxK(arr, k);

         Console.WriteLine(string.Join(", ", result));
    }
}


 
/*
run:
 
11, 9, 6
 
*/

 



answered Apr 6 by avibootz
...