- 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
C# Program to remove duplicate characters from String
Use Hashset to remove duplicate characters.
Here is the string −
string myStr = "kkllmmnnoo";
Now, use HashSet to map the string to char. This will remove the duplicate characters from a string.
var unique = new HashSet<char>(myStr);
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 myStr = "kkllmmnnoo"; Console.WriteLine("Initial String: "+myStr); var unique = new HashSet<char>(myStr); Console.Write("New String after removing duplicates: "); foreach (char c in unique) Console.Write(c); } } }
Output
Initial String: kkllmmnnoo New String after removing duplicates: klmno
Advertisements