C#  

Find second largest element in an array

Step 1: Design Page – SecondLargestArray.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SecondLargestArray.aspx.cs" Inherits="SecondLargestArray" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Find Second Largest Element in Array - Real Time Example</title>
    <style>
        body {
            font-family: Arial;
            background-color: #eef2f7;
            margin: 50px;
        }
        .container {
            width: 520px;
            margin: auto;
            background: white;
            border-radius: 8px;
            box-shadow: 0px 0px 10px #ccc;
            padding: 25px;
        }
        h2 {
            color: #1A2A80;
            text-align: center;
        }
        .form-control {
            width: 100%;
            padding: 8px;
            margin-top: 10px;
        }
        .btn {
            background-color: #7A85C1;
            color: white;
            border: none;
            padding: 10px;
            margin-top: 10px;
            border-radius: 5px;
            cursor: pointer;
            width: 100%;
        }
        .result {
            margin-top: 20px;
            font-weight: bold;
            color: #333;
            text-align: center;
            white-space: pre-line;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div class="container">
            <h2>Find Second Largest Element</h2>

            <asp:Label ID="lblInput" runat="server" Text="Enter Array Elements (comma-separated):"></asp:Label><br />
            <asp:TextBox ID="txtNumbers" runat="server" CssClass="form-control" placeholder="Example: 10, 20, 5, 30, 25"></asp:TextBox><br />

            <asp:Button ID="btnFindSecondLargest" runat="server" Text="Find Second Largest" CssClass="btn" OnClick="btnFindSecondLargest_Click" /><br />

            <asp:Label ID="lblResult" runat="server" CssClass="result"></asp:Label>
        </div>
    </form>
</body>
</html>

Step 2: Backend Logic – SecondLargestArray.aspx.cs

using System;
using System.Linq;

public partial class SecondLargestArray : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnFindSecondLargest_Click(object sender, EventArgs e)
    {
        string input = txtNumbers.Text.Trim();

        if (string.IsNullOrEmpty(input))
        {
            lblResult.Text = "Please enter numbers separated by commas.";
            lblResult.ForeColor = System.Drawing.Color.Red;
            return;
        }

        try
        {
            // Convert comma-separated string to integer array
            int[] numbers = input.Split(',').Select(x => Convert.ToInt32(x.Trim())).ToArray();

            if (numbers.Length < 2)
            {
                lblResult.Text = "Please enter at least two numbers.";
                lblResult.ForeColor = System.Drawing.Color.Red;
                return;
            }

            // Initialize largest and second largest
            int firstLargest = int.MinValue;
            int secondLargest = int.MinValue;

            // Loop through array to find largest and second largest
            foreach (int num in numbers)
            {
                if (num > firstLargest)
                {
                    secondLargest = firstLargest;
                    firstLargest = num;
                }
                else if (num > secondLargest && num != firstLargest)
                {
                    secondLargest = num;
                }
            }

            if (secondLargest == int.MinValue)
            {
                lblResult.Text = "All elements are equal, no second largest found.";
                lblResult.ForeColor = System.Drawing.Color.OrangeRed;
            }
            else
            {
                lblResult.Text = $"Array Elements: {string.Join(", ", numbers)}\n\n" +
                                 $"Largest Element: {firstLargest}\n" +
                                 $"Second Largest Element: {secondLargest}";
                lblResult.ForeColor = System.Drawing.Color.Green;
            }
        }
        catch
        {
            lblResult.Text = "Invalid input! Please enter only numbers separated by commas.";
            lblResult.ForeColor = System.Drawing.Color.Red;
        }
    }
}

Real-Time Example Flow

  1. Run the project and open SecondLargestArray.aspx.

  2. Enter the array:
    10, 20, 5, 30, 25

  3. Click Find Second Largest.

  4. Output:

    Array Elements: 10, 20, 5, 30, 25
    
    Largest Element: 30
    Second Largest Element: 25
    

Explanation

StepDescription
1Accepts array input from the user.
2Converts the comma-separated string into an integer array.
3Uses two variables – firstLargest and secondLargest.
4Traverses the array once to find the two largest values.
5Displays both results clearly.

Example Input / Output Table

InputOutput
10, 20, 30, 40Second Largest: 30
5, 5, 5All elements are equal
50, 20, 10, 40Second Largest: 40
9, 2, 7, 3Second Largest: 7

Algorithm

1. Read array elements.
2. Initialize firstLargest = secondLargest = smallest integer.
3. Loop through each element:
     If current > firstLargest:
         secondLargest = firstLargest
         firstLargest = current
     Else if current > secondLargest and current != firstLargest:
         secondLargest = current
4. Print secondLargest.