Computer >> Máy Tính >  >> Lập trình >> C#

Chương trình C # để loại bỏ tất cả các từ trùng lặp khỏi một câu nhất định

Đặt một chuỗi có các từ trùng lặp.

string str = "One Two Three One";

Ở trên, bạn có thể thấy từ “Một” xuất hiện hai lần.

Để loại bỏ các từ phức tạp, bạn có thể thử chạy đoạn mã sau trong C # -

Ví dụ

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();
   }
}

Đầu ra

One Two Three One
After removing duplicate words...
one three two