- 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 all duplicates words from a given sentence
Set a string with duplicate words.
string str = "One Two Three One";
Above, you can see the word “One” comes twice.
To remove dulicate words, you can try to run the following code in C# −
Example
using System; using System.Linq; public class Program { public static void Main() { string str = "One Two Three One"; string[] arr = str.Split(' '); Console.WriteLine(str); var a = from k in arr orderby k select k; Console.WriteLine("After removing duplicate words..."); foreach(string res in a.Distinct()) { Console.Write(" " + res.ToLower()); } Console.ReadLine(); } }
Output
One Two Three One After removing duplicate words... one three two
Advertisements