首页 > 代码库 > linq 获取不重复数据,重复数据 var unique = arr.GroupBy(o => o).Where(g => g.Count() == 1) .Select(g => g.ElementAt(0));

linq 获取不重复数据,重复数据 var unique = arr.GroupBy(o => o).Where(g => g.Count() == 1) .Select(g => g.ElementAt(0));

static void Main(string[] args)
{
int[] arr = { 1, 3, 3, 3, 3, 4, 5, 4, 5, 8, 9, 3 };

//不重复
var unique = arr.GroupBy(o => o).Where(g => g.Count() == 1)
.Select(g => g.ElementAt(0));
var uniqueList = arr.Distinct<int>().ToList();
foreach(var a in uniqueList)
{
Console.WriteLine(a);
}

Console.WriteLine("\n\n\n");
foreach (var a in unique)
{
System.Diagnostics.Debug.WriteLine(a);
Console.WriteLine(a);
}

Console.ReadLine();

 

 

 


//重复元素
var duplicate = arr.GroupBy(o => o)
.Where(g => g.Count() > 1)
.Select(g => g.ElementAt(0));
foreach(var a in duplicate)
{
Console.WriteLine(a);
}

Console.ReadLine();

linq 获取不重复数据,重复数据 var unique = arr.GroupBy(o => o).Where(g => g.Count() == 1) .Select(g => g.ElementAt(0));