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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
ketan borsdiyaRank #2,258 · 624 reputationPosted Jul 4, 2016, 12:55 AM EDT
Rajeesh MenothRank #118 · 18.4K reputationPosted Jul 4, 2016, 2:15 AM EDT
Munesh SharmaRank #174 · 13.9K reputationPosted Jul 4, 2016, 2:16 AM EDT
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()
Rajeev PunhaniRank #551 · 4.4K reputationPosted Jul 4, 2016, 2:16 AM EDT
Hi Ramesh,
Please find the below code.
If the above solution is helpful then,accept the answer.