using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string>();
list.Add("홍길동");
list.Add("범범조조");
list.Add("범범조조");
list.Add("프로그래머");
Console.WriteLine("********************중복 제거
전 출력 결과********************");
//중복 제거 전
foreach (string str in list)
{
Console.WriteLine("{0}", str);
}
Console.WriteLine("*********************중복 제거
후 출력 결과*******************");
list = list.Distinct().ToList();
//중복 제거 후
foreach(string str in list)
{
Console.WriteLine("{0}", str);
}
}
}
}
간단히 위 소스코드를 설명 드리자면, string 형의 List 객체를 하나 선언 하였고 해당 list에 각각 "홍길동", "범범조조", "범범조조", "프로그래머" 이렇게 총 4개의 문자를 넣은 것을 확인 하실 수 있습니다.
출력 결과 화면을 보시게 되면,
첫 번째 중복을 제거 하기전에는 범범조조가 두 번 출력이 된것을 확인하실 수 있고
두 번째 출력에서는 Distinct() 메서드를 이용하여 미리 중복을 제거 함으로써, 범범조조가 한 번만 출력된 것을 확인하실 수 있습니다.