# Find a maximum and second maximum value in an array using c#.net

For Example, I have an array like { 21, -31, 36, 99, -33, 58, 87, -83, -73, 94 }, want to find out the maximum value and second maximum value from that array. Here I am having both negative and positive values.

I am listing out the simple steps to do this task easily understanble.

1) Define the array int[] myArray = { 21, -31, 36, 99, -33, 58, 87, -83, -73, 94 };

2) Initialize the variables int maxNum1 = 0, maxNum2 = 0;

3) Looping it from 0 to array length

4) Performing the if condition with myArray[loop] > maxNum1 && maxNum2 <= maxNum1.

5) Assigning maxNum1 in maxNum2 and myArray[loop] in maxNum1.

6) Else conditon for the if with myArray[loop] > maxNum2 and Assigning myArray[loop] in maxNum1.

7) Getting the out put from maxNum1 and maxNum2

``using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace ConsoleApplication3{    class Program    {        static void Main(string[] args)        {            Console.WriteLine("This is a Program to get the Maximun vlue and Second Maximum value from a given Array");            Console.WriteLine("******************************************************************");            Console.WriteLine();            int[] myArray = { 21, -31, 36, 99, -33, 58, 87, -83, -73, 94 };            int maxNum1 = 0, maxNum2 = 0;            for (int loop = 0; loop < myArray.Length; loop++)            {                if (myArray[loop] > maxNum1 && maxNum2 <= maxNum1)                {                    maxNum2 = maxNum1;                    maxNum1 = myArray[loop];                }                else if (myArray[loop] > maxNum2)                {                    maxNum2 = myArray[loop];                }            }            Console.WriteLine("First Max  : {0} and Second max in array : {1} in a given array", maxNum1, maxNum2);            Console.Read();        }    }} ``

Regards,
Naveen