C#  

Find Duplicate characters in a string

Step 1: Design Page – DuplicateCharacters.aspx

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

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Find Duplicate Characters - Real Time Example</title>
    <style>
        body {
            font-family: Arial;
            background-color: #f0f2f5;
            margin: 50px;
        }
        .container {
            width: 460px;
            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 Characters</h2>

            <asp:Label ID="lblInput" runat="server" Text="Enter a String:"></asp:Label><br />
            <asp:TextBox ID="txtInput" runat="server" CssClass="form-control" placeholder="Example: programming"></asp:TextBox><br />

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

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

Step 2: Backend Logic – DuplicateCharacters.aspx.cs

using System;
using System.Collections.Generic;

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

    protected void btnFind_Click(object sender, EventArgs e)
    {
        string input = txtInput.Text.Trim().ToLower();

        if (string.IsNullOrEmpty(input))
        {
            lblResult.Text = "Please enter a valid string.";
            lblResult.ForeColor = System.Drawing.Color.Red;
            return;
        }

        Dictionary<char, int> charCount = new Dictionary<char, int>();
        string duplicates = "";

        // Count each character occurrence
        foreach (char c in input)
        {
            if (char.IsLetter(c)) // count only letters
            {
                if (charCount.ContainsKey(c))
                    charCount[c]++;
                else
                    charCount[c] = 1;
            }
        }

        // Find duplicates
        foreach (var item in charCount)
        {
            if (item.Value > 1)
            {
                duplicates += $"'{item.Key}' appears {item.Value} times.\n";
            }
        }

        if (string.IsNullOrEmpty(duplicates))
        {
            lblResult.Text = "No duplicate characters found.";
            lblResult.ForeColor = System.Drawing.Color.Green;
        }
        else
        {
            lblResult.Text = "Duplicate Characters Found:\n" + duplicates;
            lblResult.ForeColor = System.Drawing.Color.DarkRed;
        }
    }
}

Real-Time Example Flow

  1. Open DuplicateCharacters.aspx in your browser.

  2. Enter: programming

  3. Click “Find Duplicates”.

  4. Output displays:

    Duplicate Characters Found:
    'r' appears 2 times.
    'g' appears 2 times.
    'm' appears 2 times.
    

Explanation

StepDescription
1Accept user input from textbox.
2Convert to lowercase for uniform comparison.
3Count occurrences of each letter using a dictionary.
4Identify characters that occur more than once.
5Display the list of duplicates on the page.

Sample Input / Output

InputOutput
programmingr, g, m
successs, c
hellol
sandhiyaa
worldNo duplicates