• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Saturday, August 15, 2020

How to Remove Duplicate Characters From a String in C#

 Admin     August 15, 2020     C#, Interview Question, Programming, Programs     No comments   

In this article, I am going to discuss how to remove duplicate characters from a string in C# with some examples. Please read our previous article where we discussed How to Reverse Each Word in a Given String in C# with some examples. As part of this article, we are going to use the following three approaches to remove the duplicate characters from the given string C#.
  1. The simple way of Implementation to remove duplicate characters
  2. Using HashSet to Remove Duplicate Characters from a string
  3. Using LINQ to Remove Duplicate Characters from a string
Program Description:
Here, the user will input a string and that string may contain some characters multiple times. Our requirement is to have a character only once in the string. So here we need to remove the duplicate characters from the string. For better understanding, please have a look at the following diagram which shows the input and the expected output.
How to Remove Duplicate Characters from a String in C#

The Simple way of Implementation:
In the following program, we are looping through all the characters of the input and string and then checking whether that character is already there in the result string. If it is already there then we simply ignore it else we add that character to the end of the result string.
using System;
using System.Linq;
namespace LogicalPrograms
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a String : ");
            string inputString = Console.ReadLine();
            string resultString = string.Empty;
            for (int i = 0; i < inputString.Length; i++)
            {
                if (!resultString.Contains(inputString[i]))
                {
                    resultString += inputString[i];
                }
            }
            Console.WriteLine(resultString);
            Console.ReadKey();
        }
    }
}
Here, we use the Contains method which will check whether a character is present or not.

Using HashSet to Remove Duplicate Characters:
In the following example, we use HashSet to map the string to char. This will remove the duplicate characters from a string.
using System;
using System.Collections.Generic;
namespace LogicalPrograms
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a String : ");
            string inputString = Console.ReadLine();
            string resultString = string.Empty;
            var unique = new HashSet<char>(inputString);
            foreach (char c in unique)
            {
                resultString += c;
            }
            Console.WriteLine("After Removing Duplicates : " + resultString);
            Console.ReadKey();
        }
    }
}
Using LINQ to Remove Duplicate Characters From a String:
In the following example, first we are converting the string into a character array and then we are applying the LINQ Distinct method to remove the duplicate characters. Finally, we convert the character array into a string which should contain the unique characters.
using System.Linq;
using System;
namespace LogicalPrograms
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a String : ");
            string inputString = Console.ReadLine();
            
            var uniqueCharArray = inputString.ToCharArray().Distinct().ToArray();
            var resultString = new string(uniqueCharArray);
            Console.WriteLine("After Removing Duplicates : " + resultString);
            Console.ReadKey();
        }
    }
}
Output:
Using LINQ to Remove Duplicate characters from a string in C#

In the next article, I am going to discuss how to find all possible substrings of a given string in C# with some examples. I hope now you understood How to remove duplicate characters from a string with different ways.

Summary:
I hope this post will be helpful to write a program to Remove Duplicate Characters From a String in C#
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

0 comments:

Post a Comment

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • What is Dependency Injection(DI)
    Hi friends! Today we are going to learn about Dependency Injection and in our last session we have come across Static classes and where it s...
  • C# Programming Examples on Sorting
    Today i am going to tell you some of the Sorting programming questions in C#. Q1- Write a C# program to perform Selection sort. Ans:  Sel...
  • Calling Web API Service in a Cross-Domain Using jQuery AJAX
    In this article, I am going to discuss Calling Web API Service in a Cross-Domain Using jQuery AJAX . Please read our previous article befor...
  • ViewBag in ASP.NET Core MVC
    In this article, I am going to discuss the use of ViewBag in ASP.NET Core MVC application with examples. Please read our previous article ...
  • Recursion And Back Tracking
    In this article, I am going to discuss Recursion And BackTracking in detail. Please read our previous article where we discussed Master Th...
  • What is Abstract Class and When we should use Abstract Class
    Hi friends! In our previous sessions we have seen  Difference Between Class and Struct . And in our last session  we learnt Usability of Sec...
  • Binary to Decimal Conversion in C# with Examples
    In this article, I am going to discuss the Binary to Decimal Conversion in C# with some examples. Please read our previous article where w...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape