Second Highest element in an array

Write a program to Find Second Highest element in an array.


For example:

Input:
Arr = {1,7,4,2,1,3}

Output:
4

Solution:

using System;
using System.Linq;

namespace SecondHigh
{
    class Program
    {
        static void Main()
        {

            var arr = new int[] { 100, -3, 95, 100, 95, 177, -5, -4, 177, 101 };

            var max = arr.Max();
            var min = arr.Min();

            foreach (var t in arr)
            {
                if (t > min && max > t)
                    min = t;
            }

            Console.Write($"Second max number from an array: {min}");
            Console.ReadLine();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *