如何在列表中插入元素没有重复? [英] how insert element without duplicates in list?
本文介绍了如何在列表中插入元素没有重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
List<int> li = new List<int>() { 1, 1, 1, 1, 1, 1, 1, 2, 2 };
List<int> New = new List<int>();
foreach (int item in li)
New.Add(item);
推荐答案
嗨..
尝试下面的代码行,在你的新名单中有不同的值叫做distinct。
Hi..
Try below code of line to have distinct values in your new list called distinct..
List<int> li = new List<int>() { 1, 1, 1, 1, 1, 1, 1, 2, 2 };// your original list containing duplicates</int></int>
List<int> distinct = li.Distinct().ToList();// new list containing only distinct values from above list..</int>
希望它工作正常..
Hope it works fine..
试试改为使用HashSet(除非顺序很重要)。
Try a HashSet instead (unless order is significant).
List<int> li = new List<int>() { 1, 1, 1, 1, 1, 1, 1, 2, 2 };
List<int> New = new List<int>();
foreach(int i in li)
{
if (!New.Contains(i))
New.Add(i);
}
这篇关于如何在列表中插入元素没有重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文