none
Get index of the largest element in array - C# RRS feed

  • Question

  • Hello,

    In the below code I want to get index of the largest element in array. in this example I expected have 14.

    Thanks in advance

    using System;
    using System.Linq;
    
    
    namespace CSharp
    {
        class Class1
        {
            public static void Main()
            {
                int[] num = { 3,45,56,67,67,7,23,6,6,7,78,54,4,34,342};
                Console.WriteLine(num.Max());
            }
        }
    
    }

    Thursday, November 7, 2019 7:51 AM

Answers

  • For example following is one solution:

    int index = -1;
    
    if (num != null && num.Length > 0)
       index = Array.IndexOf(num, num.Max());

    • Marked as answer by Arash_89 Thursday, November 7, 2019 8:19 AM
    Thursday, November 7, 2019 8:16 AM