Monday, March 28, 2016

max value in array

           int[] anArray = { 2, 5, 10, 9, 10, 6 };

            int? maxVal = null; //nullable so this works even if you have all super-low negatives
            int index = -1;
            for (int i = 0; i < anArray.Length; i++)
            {
                int thisNum = anArray[i];
                if (!maxVal.HasValue || thisNum > maxVal.Value)
                {
                    maxVal = thisNum;
                    index = i;
                }
            }
            Console.WriteLine(maxVal);

No comments:

Post a Comment

Find the value from array when age is more than 30

 const data = [   { id: 1, name: 'Alice', age: 25 },   { id: 2, name: 'Bob', age: 30 },   { id: 3, name: 'Charlie', ...