C# Program to Remove Duplicate Characters from a String

In this article, I will be writing a C# Program to remove duplicate characters from a String. You need to learn basic concepts of C# Language to write this program.

You must learn the following concepts related to C# Language before writing this program:

  • Conditional If Else
  • For Loop in C#

Algorithm

Here’s the algorithm to write a C# Program to Remove Duplicate Characters from a String:

STEP 1. Arrange the elements in the correct order.
STEP 2. Remove duplicates by comparing the current character to the preceding character in a loop.
STEP 3. At the end of the generated string, remove any excess characters.

Program

Here’s the C# Program to Remove Duplicate Characters from a String:

using System;
					
public class Program
{
	public static void Main()
	{
		String inputStr, resultStr = String.Empty;
		
		Console.WriteLine("Enter a string:");
		inputStr = Console.ReadLine();
		
		for(int i = 0; i < inputStr.Length; i++){
			if(!resultStr.Contains(inputStr[i]+"")){
				resultStr += inputStr[i];
			}
		}
		
		Console.WriteLine("Final String: "+resultStr);
	}
}

Output

Here’s the output of the above program:

Resources

Here are the resources related to our program. You can download these files and run the program on your own PC or any other device.

Related Programs

  1. C# Program to Reverse a String
  2. C# Program to Print ASCII Values
  3. C# Program to print Alphabet Triangle

Similar Posts

Leave a Reply

Your email address will not be published.