C#  

Find duplicate elements in an array using C#

Step 1: Design Page – FindDuplicateArray.aspx

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

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Find Duplicate Elements in an Array - Real Time Example</title>
    <style>
        body {
            font-family: Arial;
            background-color: #f0f2f5;
            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 Duplicate Elements in Array</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: 1, 2, 3, 2, 4, 1"></asp:TextBox><br />

            <asp:Button ID="btnFindDuplicates" runat="server" Text="Find Duplicates" CssClass="btn" OnClick="btnFindDuplicates_Click" /><br />

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

Step 2: Backend Logic – FindDuplicateArray.aspx.cs

using System;
using System.Linq;
using System.Collections.Generic;

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

    protected void btnFindDuplicates_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 values to integer array
            int[] numbers = input.Split(',').Select(x => Convert.ToInt32(x.Trim())).ToArray();
            List<int> duplicates = new List<int>();

            // Find duplicates manually using nested loop
            for (int i = 0; i < numbers.Length; i++)
            {
                for (int j = i + 1; j < numbers.Length; j++)
                {
                    if (numbers[i] == numbers[j] && !duplicates.Contains(numbers[i]))
                    {
                        duplicates.Add(numbers[i]);
                    }
                }
            }

            if (duplicates.Count > 0)
            {
                lblResult.Text = $"Array Elements: {string.Join(", ", numbers)}\n\n" +
                                 $"Duplicate Elements Found: {string.Join(", ", duplicates)}";
                lblResult.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                lblResult.Text = $"Array Elements: {string.Join(", ", numbers)}\n\nNo Duplicate Elements Found.";
                lblResult.ForeColor = System.Drawing.Color.Blue;
            }
        }
        catch
        {
            lblResult.Text = "Invalid input! Please enter only numbers separated by commas.";
            lblResult.ForeColor = System.Drawing.Color.Red;
        }
    }
}

Real-Time Example Flow

  1. Run FindDuplicateArray.aspx in your browser.

  2. Enter:
    1, 2, 3, 2, 4, 1

  3. Click “Find Duplicates”.

  4. Output:

    Array Elements: 1, 2, 3, 2, 4, 1
    
    Duplicate Elements Found: 1, 2
    

Explanation

StepDescription
1The user enters comma-separated numbers.
2Convert the input into an integer array.
3Use nested loops to compare each element with every other element.
4Add duplicate numbers to a list only once using a condition check.
5Display all duplicate elements found.

Example Input / Output Table

InputOutput
1, 2, 3, 2, 4, 1Duplicates: 1, 2
5, 6, 7, 8No duplicates
10, 20, 10, 30, 20, 40Duplicates: 10, 20
3, 3, 3, 3Duplicates: 3

Algorithm

1. Read array elements.
2. For each element i in the array:
      Compare it with all elements after it (j = i+1).
      If numbers[i] == numbers[j] and not already in duplicates:
          Add to duplicate list.
3. Display the duplicate elements.