find the 2nd highest number in an integer array?
write a program to find a second highest number in an integer array?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Rajeev PunhaniPosted Jul 4, 2016, 6:16 AM
Hi Ramesh,
Please find the below code.
If the above solution is helpful then,accept the answer.
Munesh SharmaPosted Jul 4, 2016, 6:16 AM
int[] myArray = new int[] { 0, 1, 2, 3, 13, 8, 5 };
int largest = int.MinValue;
int second = int.MinValue;
foreach (int i in myArray)
{
if (i > largest)
{
second = largest;
largest = i;
}
else if (i > second)
second = i;
}
System.Console.WriteLine(second);
OR
Try this (using LINQ):
int secondHighest = (from number in test
orderby number descending
select number).Distinct().Skip(1).First()
Rajeesh MenothPosted Jul 4, 2016, 6:15 AM
ketan borsdiyaPosted Jul 4, 2016, 4:55 AM