C# 去掉List重复元素的方法
因为用到list,要去除重复数据,尝试了几种方法。记录于此。。。
测试数据:
List<string> li1 = new List<string> { "8", "8", "9", "9" ,"0","9"}; List<string> li2 = new List<string> { "张三", "张三", "李四", "张三", "王五", "李四" }; List<string> li3 = new List<string> { "A", "A", "C", "A", "C", "D" }; List<string> li4 = new List<string> { "12", "18", "19", "19", "10", "19" };
方法一:
HashSet<string> hs = new HashSet<string>(li1); //此时已经去掉重复的数据保存在hashset中
方法二:
for (int i = 0; i < li2.Count; i++) //外循环是循环的次数 { for (int j = li2.Count - 1 ; j > i; j--) //内循环是 外循环一次比较的次数 { if (li2[i] == li2[j]) { li2.RemoveAt(j); } } }
方法三:
//把相同的用null代替。 for (int i = 0; i < li3.Count; i++) { for (int j = 0; j < li3.Count; j++) { if (i == j) continue; if (li3[i] == li3[j]) { li3[j] = "null"; } } }
方法四:
//这方法跟上面的一样,只是变了逻辑 for (int i = 0; i < li4.Count - 1; i++) { for (int j = 0; j < li4.Count ; j++) { if (i != j) { if (li4[i] == li4[j]) { li4[j] = "null"; } } } }
最后输出看结果
Console.WriteLine("li1去除重复后的值为"); hs.ToList().ForEach(item => Console.WriteLine(item)); Console.WriteLine("li2去除重复后的值为"); li2.ForEach(item => Console.WriteLine(item)); Console.WriteLine("li3去除重复后的值为"); li3.ForEach(item => Console.WriteLine(item)); Console.WriteLine("li4去除重复后的值为"); li4.ForEach(item => Console.WriteLine(item));
null我没去掉。用的时候去掉即可。
当然。还有许多办法。比如linq Distinct 等等都可以,看看网上的这个例子:去掉modelList中title重复的内容,不区分大小写
class Program { static void Main(string[] args) { List<Model> modelList = new List<Model>() { new Model() { ID = 1, Title = "abcde" }, new Model() { ID = 2, Title = "ABCDE" }, new Model(){ ID = 3, Title = "AbcdE" }, new Model() { ID = 4, Title = "A" }, new Model() { ID = 5, Title = "a" } }; Console.Read(); } } public class Model { public int ID { get; set; } public string Title { get; set; } }
解决方案一:这里比较的前提是对象的哈希代码相等。否则不会比较,因为哈希代码不相等。两个对象显然不相等
//定义一个类继承IEqualityComparer接口 public class ModelComparer : IEqualityComparer<Model> { public bool Equals(Model x, Model y) { return x.Title.ToUpper() == y.Title.ToUpper(); } public int GetHashCode(Model obj) { return obj.Title.ToUpper().GetHashCode(); } }
调用:
modelList = modelList.Distinct(new ModelComparer()).ToList();
解决方案二:
var title = modelList.GroupBy(m => m.Title.ToLower().Trim()).Select(m => new { ID = m.FirstOrDefault().ID }); modelList = modelList.Where(m => title.Select(mo => mo.ID).Contains(m.ID)).ToList(); foreach (var item in modelList) { Console.WriteLine(item.ID + "\t" + item.Title); }
当然。如果你仅仅比较两个值是否相等。
List<string> li1 = new List<string> { "8", "8", "9", "8", "0", "9" }; li1 = li1.Distinct().ToList();