Friday, July 12, 2013

Algorithm: Find second highest number from Array


 static void Main(string[] args)
        {
            int[] array = new int[] { 0, 6, 2, 7, 2, 6 };
            int first = int.MinValue;
            int second = int.MinValue;

            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] > first)
                {
                    second = first;
                    first = array[i];                  
                }
                else if (array[i] < first && array[i] > second)
                {
                    second = array[i];
                }
            }          

            Console.WriteLine(second);
            Console.ReadKey();
        }

Output:
6

No comments:

Post a Comment