List<string[]> 如何去重,代码如下:
static void Main(string[] args) { List<string[]> list = new List<string[]>(); list.Add(new string[] { "1", "2", "3" }); list.Add(new string[] { "1" }); list.Add(new string[] { "1", "2", "3" }); list.Add(new string[] { "1" }); list.Add(new string[] { "1" }); List<string> strList = new List<string>(); foreach (var item in list) { string s = string.Join(",", item); strList.Add(s); } //要删除的元素的下标集合 List<int> removeIndexList = new List<int>(); if (list.Count >= 2) //确保下标i不越界 { string currentStr = string.Empty; for (int i = 0; i < strList.Count; i++) { currentStr = strList[i]; for (int j = i + 1; j < strList.Count; j++) { if (currentStr == strList[j]) { //添加到要删除的索引集合removeIndexList中 removeIndexList.Add(j); } } } removeIndexList = removeIndexList.Distinct().ToList();////去除重复的索引 //添加到要删除的对象集合 List<string[]> removeList = new List<string[]>(); foreach (var index in removeIndexList) { removeList.Add(list[index]); } //遍历要删除对象的集合,删除原集合中的对象 foreach (var item in removeList) { list.Remove(item); } foreach (var item in list) { string s = string.Join(",", item); Console.WriteLine(s); } Console.ReadKey(); } }