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;
}
}

More from C# Programming Tutorial

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details