- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove all duplicates from a given string in C#
Here is the string.
string str = "ppqqrr";
Now, use Hashset to map the string to char. This will remove the duplicate characters from a string.
var res = new HashSet<char>(str);
Let us see the complete example −
Example
using System; using System.Linq; using System.Collections.Generic; namespace Demo { class Program { static void Main(string[] args) { string str = "ppqqrr"; Console.WriteLine("Initial String: "+str); var res = new HashSet<char>(str); Console.Write("New String after removing duplicates:"); foreach (char c in res){ Console.Write(c); } } } }
Output
Initial String: ppqqrr New String after removing duplicates:pqr
- Related Articles
- Remove all duplicates from a given string in Python
- C# program to remove all duplicates words from a given sentence
- Python program to remove all duplicates word from a given sentence.
- Java program to remove all duplicates words from a given sentence
- Remove All Adjacent Duplicates In String in Python
- Remove All Adjacent Duplicates in String II in C++
- Print all distinct permutations of a given string with duplicates in C++
- Java program to remove all the white spaces from a given string
- Remove duplicates from a List in C#
- Write a program in C++ to remove duplicates from a given array of prime numbers
- How can we remove all the prefixes or suffixes from a given string in MySQL?
- Remove all whitespaces from string - JavaScript
- Remove Duplicates from Sorted Array in Python
- Remove Duplicates from Sorted List in C++
- Remove a Given Word from a String using C++

Advertisements