डुप्लीकेट शब्दों के साथ एक स्ट्रिंग सेट करें।
string str = "One Two Three One";
उपरोक्त, आप देख सकते हैं कि शब्द "One" दो बार आता है।
डुप्लीकेट शब्दों को हटाने के लिए, आप C# में निम्नलिखित कोड चलाने का प्रयास कर सकते हैं:
उदाहरण
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();
}
}
आउटपुट
One Two Three One After removing duplicate words... one three two