w3resource

C#: Find the second largest element in an array

C# Sharp Array: Exercise-16 with Solution

Write a C# Sharp program to find the second largest element in an array.

C# Sharp: Find the second largest element in an array

Sample Solution:-

C# Sharp Code:

using System;

public class Exercise16  
{  
    public static void Main() 
    {
        int n, i, j = 0, lrg, lrg2nd; // Declare variables for array size, counting, and finding largest elements
        int[] arr1 = new int[50]; // Declare an array to store integers

        // Display a message prompting the user to input the size of the array
        Console.Write("\n\nFind the second largest element in an array :\n");
        Console.Write("-----------------------------------------\n");  

        Console.Write("Input the size of array : ");
        n = Convert.ToInt32(Console.ReadLine()); // Read the size of the array entered by the user

        /* Store values into the array */
        Console.Write("Input {0} elements in the array :\n", n);
        for (i = 0; i < n; i++)
        {
            Console.Write("element - {0} : ", i);
            arr1[i] = Convert.ToInt32(Console.ReadLine()); // Store user input in the array
        }

        /* Find the location of the largest element in the array */
        lrg = 0;
        for (i = 0; i < n; i++)
        {
            if (lrg < arr1[i])
            {
                lrg = arr1[i]; // Find the largest element
                j = i; // Store the index of the largest element
            }
        }

        /* Find the second largest element in the array by ignoring the largest element */
        lrg2nd = 0;
        for (i = 0; i < n; i++)
        {
            if (i == j)
            {
                i++; /* Ignore the largest element */
                i--;
            }
            else
            {
                if (lrg2nd < arr1[i])
                {
                    lrg2nd = arr1[i]; // Find the second largest element
                }
            }
        }

        Console.Write("The Second largest element in the array is :  {0} \n\n", lrg2nd); // Display the second largest element
    }
}

Sample Output:

Find the second largest element in an array :                                                                 
-----------------------------------------                                                                     
Input the size of array : 5                                                                                   
Input 5 elements in the array :                                                                               
element - 0 : 2                                                                                               
element - 1 : 4                                                                                               
element - 2 : 6                                                                                               
element - 3 : 8                                                                                               
element - 4 : 10                                                                                              
The Second largest element in the array is :  8 

Flowchart:

Flowchart: Find the second largest element in an array

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to delete an element at desired position from an array.
Next: Write a program in C# Sharp to find the second smallest element in an array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.