In this article, we’ll learn how to remove duplicate characters from a String in C# using multiple examples.
Table of Contents
Understanding the Problem
Before diving into the code, let’s make sure we understand the problem. Removing duplicate characters means eliminating any characters that appear more than once in a given string.
For example, in the string “programming,” Our goal is to eliminate duplicate characters and transform the string into “progamin
“.
Method 1: Remove duplicate characters using a HashSet
One of the most efficient ways to remove duplicate characters from a String is by using a HashSet. The HashSet is a collection that does not allow duplicate elements.
Here is a simple C# code snippet demonstrating this approach:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
string input = "programming";
string result = RemoveDuplicatesHashSet(input);
Console.WriteLine("Original String: " + input);
Console.WriteLine("String without Duplicates: " + result);
}
static string RemoveDuplicatesHashSet(string input)
{
// Creating a HashSet automatically removes duplicates.
HashSet<char> uniqueChars = new HashSet<char>(input);
string result = "";
// Adding each unique character to the result using "foreach" loop.
foreach (char c in uniqueChars)
{
result += c;
}
return result;
}
}
Output:
Original String: programming
String without Duplicates: progamin
Method 2: Remove duplicate characters from a String Using LINQ
For those who prefer a more concise solution, LINQ provides an elegant way to remove duplicate characters from a string. Make sure to include the using System.Linq;
directive in your code.
Here is an example:
using System;
using System.Linq;
class Program
{
static void Main()
{
string input = "programming";
string result = RemoveDuplicatesLinq(input);
Console.WriteLine("Original String: " + input);
Console.WriteLine("String without Duplicates: " + result);
}
static string RemoveDuplicatesLinq(string input)
{
// We use the Distinct() method to get unique characters
// Then, we convert the result back to a string using ToArray() and the new string constructor
string result = new string(input.Distinct().ToArray());
return result;
}
}
Output:
Original String: programming
String without Duplicates: progamin
Method 3: Using String.Contains Method
Using the String.Contains
method is another approach to remove duplicate characters from a string in C#.
Below is a simple program demonstrating this method:
using System;
class Program
{
static void Main()
{
string input = "Google";
string result = RemoveDuplicatesContains(input);
Console.WriteLine("Original String: " + input);
Console.WriteLine("String without Duplicates: " + result);
}
static string RemoveDuplicatesContains(string input)
{
string result = "";
foreach (char c in input)
{
if (!result.Contains(c))
{
result += c;
}
}
return result;
}
}
Output:
Original String: Google
String without Duplicates: Gogle
Code Explanation:
- We use a
foreach
loop to iterate through each character in the input string. - The
String.Contains
method is used to check if the character is already present in the result string. - If the character is not found in the result string, it is appended to the result.
While this approach is straightforward to remove duplicate characters from a string in C#, it’s worth noting that it may not be as efficient as the HashSet or LINQ methods, especially for large strings as the String.Contains method has a linear time complexity.
However, for smaller strings or simple use cases, it provides a readable and easy-to-understand solution.
Programs you might also like:
- Leap Year Program in C, C++, C#, JAVA, PYTHON, and PHP
- Bubble sort programs in C, C++, JAVA, and PYTHON
- Program to copy all elements of an array into another array
- Program to print prime numbers from 1 to N
- Different Ways to Calculate Factorial in C# (with Full Code Examples)
- Fibonacci sequence: Fibonacci series in C# (with examples)
- Program to Convert Fahrenheit to Celsius: Algorithm, Formula, and Code Examples
- Converting Celsius to Fahrenheit in C#
- Different Ways to Calculate Factorial in C# (with Full Code Examples)
- C# Tutorial
- Difference Between Array And ArrayList In C#: Choosing the Right Collection - May 28, 2024
- C# Program to Capitalize the First Character of Each Word in a String - February 21, 2024
- C# Program to Find the Longest Word in a String - February 19, 2024