Get the index of the largest value in the array. - CSharp System

CSharp examples for System:Array Index

Description

Get the index of the largest value in the array.

Demo Code

// Licensed under the Apache License, Version 2.0 (the "License");
using System.Collections.Generic;
using System;//from   w  w  w  .  j a va  2s .c o m

public class Main{
        /// <summary>
        /// Get the index of the largest value in the array.
        /// </summary>
        /// <param name="data">The array to search.</param>
        /// <returns>The index.</returns>
        public static int IndexOfLargest(double[] data)
        {
            int result = -1;

            for (int i = 0; i < data.Length; i++)
            {
                if (result == -1 || data[i] > data[result])
                    result = i;
            }

            return result;
        }
}

Related Tutorials