Mid
From PDF
Coding Scenarios
C# Coding Interview
Second highest number in array?
Logic
- Track highest and second highest in one loop.
int[] arr = { 10, 5, 20, 8 };
int first = int.MinValue, second = int.MinValue;
foreach (int num in arr)
{
if (num > first)
{
second = first;
first = num;
}
else if (num > second && num != first)
{
second = num;
}
}Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png