Write A Program To Find Second Largest Number In An Array In C#

In this C# tutorial, I have explained how to find the second largest number in an array using C#. Let us write a program to find the second largest number in an array in c#.

To find the second largest number in an array using C#, you can initialize two variables to store the largest and second largest numbers. Iterate through the array, updating these variables accordingly. If an element is greater than the largest, update both variables; if it’s between the largest and second largest, update only the second largest. This approach ensures you find the second largest number in a single pass through the array.

Find the Second Largest Number in an Array Using C#

Now, let us check an efficient way to find the second largest number in an array using C#. The most straightforward approach is to traverse the array twice: first, to find the largest number, and second, to find the largest number that’s smaller than the first.

Let us check step by step:

Step 1: Initialize Variables

We need two variables to keep track of the largest and the second largest numbers in C#. Initially, we can set them to the minimum value possible.

int largest = int.MinValue;
int secondLargest = int.MinValue;

Step 2: Traverse the Array

We’ll iterate through the array, updating our two variables based on the current element.

foreach (int number in array)
{
    if (number > largest)
    {
        secondLargest = largest;
        largest = number;
    }
    else if (number > secondLargest && number != largest)
    {
        secondLargest = number;
    }
}

Step 3: Handle Special Cases

It’s important to consider cases where the array might have duplicates of the largest number or if the array is too small.

if (secondLargest == int.MinValue)
{
    Console.WriteLine("No second largest value, or the array is too small.");
}
else
{
    Console.WriteLine($"The second largest number is {secondLargest}");
}

Here’s the complete C# program to find the second largest number in an array:

using System;

class Program
{
    static void Main()
    {
        int[] array = { 12, 35, 1, 10, 34, 1 };
        int largest = int.MinValue;
        int secondLargest = int.MinValue;

        foreach (int number in array)
        {
            if (number > largest)
            {
                secondLargest = largest;
                largest = number;
            }
            else if (number > secondLargest && number != largest)
            {
                secondLargest = number;
            }
        }

        if (secondLargest == int.MinValue)
        {
            Console.WriteLine("No second largest value, or the array is too small.");
        }
        else
        {
            Console.WriteLine($"The second largest number is {secondLargest}");
        }
    }
}

You can see the output after I ran the code using the Visual Studio console application.

write a program to find second largest number in an array in c#

Conclusion

In this C# tutorial, I have explained how to find the second largest number in an array using C#, and also, you got to know how to write a program to find the second largest number in an array in C#.

You may also like: