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
C# Program to remove duplicate characters from String
Removing duplicate characters from a string is a common programming task in C#. There are several approaches to achieve this, with HashSet being one of the most efficient methods due to its automatic handling of unique elements.
Using HashSet to Remove Duplicates
The HashSet<char> collection automatically maintains unique elements, making it perfect for removing duplicate characters from a string −
using System;
using System.Linq;
using System.Collections.Generic;
class Program {
static void Main(string[] args) {
string myStr = "kkllmmnnoo";
Console.WriteLine("Initial String: " + myStr);
var unique = new HashSet<char>(myStr);
string result = new string(unique.ToArray());
Console.WriteLine("New String after removing duplicates: " + result);
}
}
The output of the above code is −
Initial String: kkllmmnnoo New String after removing duplicates: klmno
Using LINQ Distinct Method
Another elegant approach uses LINQ's Distinct() method to filter unique characters −
using System;
using System.Linq;
class Program {
static void Main(string[] args) {
string myStr = "programming";
Console.WriteLine("Initial String: " + myStr);
string result = new string(myStr.Distinct().ToArray());
Console.WriteLine("New String after removing duplicates: " + result);
}
}
The output of the above code is −
Initial String: programming New String after removing duplicates: progamin
Using StringBuilder with Contains Check
For preserving the first occurrence order without using LINQ, you can use a StringBuilder with a contains check −
using System;
using System.Text;
class Program {
static void Main(string[] args) {
string myStr = "hello world";
Console.WriteLine("Initial String: " + myStr);
StringBuilder sb = new StringBuilder();
foreach (char c in myStr) {
if (sb.ToString().IndexOf(c) == -1) {
sb.Append(c);
}
}
string result = sb.ToString();
Console.WriteLine("New String after removing duplicates: " + result);
}
}
The output of the above code is −
Initial String: hello world New String after removing duplicates: helo wrd
Comparison of Methods
| Method | Time Complexity | Preserves Order | Memory Usage |
|---|---|---|---|
| HashSet | O(n) | No | Efficient |
| LINQ Distinct | O(n) | Yes | Efficient |
| StringBuilder + Contains | O(n²) | Yes | Good |
Conclusion
The HashSet approach is most efficient for removing duplicates when order doesn't matter, while LINQ's Distinct() method is ideal when you need to preserve the original character order. Choose the method based on your specific requirements for performance and ordering.
