중복 단어가 있는 문자열을 설정합니다.
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