C#  

Remove duplicate elements from an array in C#

Step 1: Design Page – RemoveDuplicatesArray.aspx

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

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Remove Duplicates from 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>Remove Duplicates from 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="btnRemoveDuplicates" runat="server" Text="Remove Duplicates" CssClass="btn" OnClick="btnRemoveDuplicates_Click" /><br />

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

Step 2: Backend Logic – RemoveDuplicatesArray.aspx.cs

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

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

    protected void btnRemoveDuplicates_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();

            // Manual logic to remove duplicates
            List<int> uniqueList = new List<int>();

            for (int i = 0; i < numbers.Length; i++)
            {
                bool exists = false;

                // Check if the number already exists in uniqueList
                for (int j = 0; j < uniqueList.Count; j++)
                {
                    if (numbers[i] == uniqueList[j])
                    {
                        exists = true;
                        break;
                    }
                }

                // If not exists, add it to the list
                if (!exists)
                {
                    uniqueList.Add(numbers[i]);
                }
            }

            lblResult.Text = $"Original Array: {string.Join(", ", numbers)}\n\n" +
                             $"Array After Removing Duplicates: {string.Join(", ", uniqueList)}";
            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 RemoveDuplicatesArray.aspx in your browser.

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

  3. Click “Remove Duplicates”

  4. Output:

    Original Array: 1, 2, 3, 2, 4, 1, 5, 3
    
    Array After Removing Duplicates: 1, 2, 3, 4, 5
    

Explanation

StepDescription
1Accepts comma-separated user input.
2Converts it into an integer array.
3Creates a List<int> to store unique numbers.
4Checks each element manually using nested loops.
5Adds only non-duplicate elements to the new list.
6Displays the result clearly.

Example Input / Output Table

InputOutput
1, 2, 3, 2, 4, 11, 2, 3, 4
5, 5, 5, 55
10, 20, 30, 20, 1010, 20, 30
8, 9, 10, 118, 9, 10, 11

Algorithm

1. Read array elements.
2. Initialize an empty list for unique elements.
3. For each element in array:
     Check if it already exists in unique list.
     If not, add it.
4. Display the unique list.