Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to print duplicate characters in a String using C#?
Finding duplicate characters in a string is a common programming task in C#. A duplicate character is one that appears more than once in the string. We can solve this by counting the frequency of each character and then displaying those with a count greater than 1.
Using Character Frequency Array
The most efficient approach uses an integer array to store the frequency of each character. Since there are 256 possible ASCII characters, we create an array of size 256 −
using System;
class Demo {
static int maxCHARS = 256;
static void calculate(String s, int[] cal) {
for (int i = 0; i < s.Length; i++)
cal[s[i]]++;
}
public static void Main() {
String s = "Welcometomywebsite!";
Console.WriteLine("Input String: " + s);
Console.WriteLine("Duplicate characters:");
int[] cal = new int[maxCHARS];
calculate(s, cal);
for (int i = 0; i < maxCHARS; i++)
if (cal[i] > 1) {
Console.WriteLine("Character '" + (char)i + "' occurs " + cal[i] + " times");
}
}
}
The output of the above code is −
Input String: Welcometomywebsite! Duplicate characters: Character 'e' occurs 6 times Character 'm' occurs 2 times Character 'o' occurs 2 times Character 't' occurs 4 times Character 'w' occurs 2 times
How It Works
The algorithm works in three steps:
Initialize: Create an integer array of size 256 to store character frequencies
Count: Loop through each character in the string and increment its frequency in the array
Display: Print characters whose frequency is greater than 1
Using Dictionary for Better Readability
An alternative approach uses a Dictionary which is more readable and memory-efficient for sparse data −
using System;
using System.Collections.Generic;
class Program {
public static void Main() {
string input = "programming";
Console.WriteLine("Input String: " + input);
Console.WriteLine("Duplicate characters:");
Dictionary<char, int> charCount = new Dictionary<char, int>();
// Count frequency of each character
foreach (char c in input) {
if (charCount.ContainsKey(c))
charCount[c]++;
else
charCount[c] = 1;
}
// Display duplicates
foreach (var pair in charCount) {
if (pair.Value > 1) {
Console.WriteLine("Character '" + pair.Key + "' occurs " + pair.Value + " times");
}
}
}
}
The output of the above code is −
Input String: programming Duplicate characters: Character 'r' occurs 2 times Character 'g' occurs 2 times Character 'm' occurs 2 times
Case-Insensitive Duplicate Detection
To find duplicates regardless of case, convert the string to lowercase first −
using System;
using System.Collections.Generic;
class Program {
public static void Main() {
string input = "Hello World";
Console.WriteLine("Input String: " + input);
Console.WriteLine("Duplicate characters (case-insensitive):");
Dictionary<char, int> charCount = new Dictionary<char, int>();
string lowerInput = input.ToLower();
foreach (char c in lowerInput) {
if (c != ' ') { // Skip spaces
charCount[c] = charCount.ContainsKey(c) ? charCount[c] + 1 : 1;
}
}
foreach (var pair in charCount) {
if (pair.Value > 1) {
Console.WriteLine("Character '" + pair.Key + "' occurs " + pair.Value + " times");
}
}
}
}
The output of the above code is −
Input String: Hello World Duplicate characters (case-insensitive): Character 'l' occurs 3 times Character 'o' occurs 2 times
Conclusion
Finding duplicate characters in a string can be efficiently done using character frequency counting. The array-based approach is fastest for ASCII characters, while Dictionary provides better readability and handles Unicode characters well.
