Tech Point Fundamentals

Tuesday, December 14, 2021

C# Program to Remove Duplicate Characters from a String

C# Program to Remove Duplicate Characters from a String

coding-interview-question-csharp

Most of the IT companies check the coding skills and problem-solving skills as well along with the theoretical interview questions. Sometimes you are free to write the pseudo code and sometimes you are asked to write the complete program either on any paper or any editor. 


This question is asked in the coding interview to write the program. Here you can find the program as well as a live running program so that you can test the program immediately.


Watch our videos here





Question: 


How can you find and remove the duplicate characters from a string? Write a program to remove the duplicate characters from a string.




C# Program To Delete the Duplicate Characters in a String


       
 

// Author 	: Tech Point Fundamentals
// Website 	: www.techpointfunda.com
// Channel	: https://www.youtube.com/c/TechPointFundamentals
// Demo		: Removing Duplicate Characters

using System;
using System.Linq;

public class Program
{
  public static void Main()
  { 
	string inputString = "Tech Point Fundamentals";		
	
	Console.WriteLine("Input String : " + inputString);		
	Console.WriteLine("After Removing Duplicate (Case Sensitive) Characters By IndexOf Method : " + RemovingDuplicateCharacters(inputString));	
	Console.WriteLine("After Removing Duplicate (Case Sensitive) Characters By LINQ : " + RemovingDuplicateCharactersByLINQ(inputString));
  }	  	
	
public static string RemovingDuplicateCharacters(string inputString)    
{               
  string output = string.Empty;  
  
  foreach (char ch in inputString)
  {
  	if (output.IndexOf(ch) == -1)
  	{
  		output += ch;
  	}
  }  
  return output;
}    
	
public static string RemovingDuplicateCharactersByLINQ(string inputString) 
{
  return new string(inputString.ToCharArray().Distinct().ToArray());
}	
}





Output: 

Input String : Tech Point Fundamentals
After Removing Duplicate (Case Sensitive) Characters By IndexOf Method : Tech PointFudamls
After Removing Duplicate (Case Sensitive) Characters By LINQ : Tech PointFudamls




Live Demo






No comments:

Post a Comment

Please do not enter any HTML. JavaScript or spam link in the comment box.