List<string[]> 如何去重,代码如下:
static void Main(string[] args)
{ List<string[]> list = new List<string[]>(); list.Add(new string[] { "", "", "" });
list.Add(new string[] { "" });
list.Add(new string[] { "", "", "" });
list.Add(new string[] { "" });
list.Add(new string[] { "" }); 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 >= ) //确保下标i不越界
{
string currentStr = string.Empty;
for (int i = ; i < strList.Count; i++)
{
currentStr = strList[i];
for (int j = i + ; 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(); }
}
运行截图如下:
那么问题又来了,挖掘机技术……呸! 如果是List<List<string[]>>的集合又该如何去重呢?
原理是一样的把List<string[]>变成字符串,装到List<string>中,根据List<sting>重复的元素的下标索引,删除原集合中重复的元素,
代码如下:
static void Main(string[] args)
{
List<string[]> list = new List<string[]>(); list.Add(new string[]{"","",""});
list.Add(new string[] { "","" ,""});
list.Add(new string[] { "" });
list.Add(new string[] { "" });
list.Add(new string[] { "" }); List<string[]> list2 = new List<string[]>(); list2.Add(new string[] { "", "", "", "", "" });
list2.Add(new string[] { "", "", "" });
list2.Add(new string[] { "" });
list2.Add(new string[] { "" });
list2.Add(new string[] { "" }); List<string[]> list3 = new List<string[]>();
list3.Add(new string[] { "", "", "" });
list3.Add(new string[] { "", "", "" });
list3.Add(new string[] { "" });
list3.Add(new string[] { "" });
list3.Add(new string[] { "" }); List<string[]> list4= new List<string[]>(); list4.Add(new string[] { "", "", "", "", "" });
list4.Add(new string[] { "", "", "" });
list4.Add(new string[] { "" });
list4.Add(new string[] { "" });
list4.Add(new string[] { "" });
List<List<string[]>> superList = new List<List<string[]>>();
//集合list和集合list3是相同,list2和list4相同,并且list4添加了2次
superList.Add(list);
superList.Add(list2);
superList.Add(list3);
superList.Add(list4);
superList.Add(list4); List<string> strList = new List<string>();
foreach (var d in superList)
{
StringBuilder sb = new StringBuilder();
foreach (var dd in d)
{
string s = string.Join(",", dd);
sb.Append(s);
}
string str = sb.ToString();
strList.Add(str); //把superList中每个子元素拼接成一条字符串放到strList中
} //要删除的元素的下标集合
List<int> removeIndexList = new List<int>();
if (strList.Count >= ) //有2个以上的元素才有可能出现重复
{
string currentStr = string.Empty;
for (int i =; i < strList.Count; i++)
{
currentStr = strList[i];
for (int j =i+; j < strList.Count; j++)
{
if (currentStr == strList[j])
{
//添加到要删除的索引集合removeIndexList中
removeIndexList.Add(j);
}
}
}
}
removeIndexList = removeIndexList.Distinct().ToList();//去除重复的索引
//要删除的对象集合
List<List<string[]>> superRemoveList = new List<List<string[]>>();
foreach (var index in removeIndexList)
{
superRemoveList.Add(superList[index]);
} foreach (var item in superRemoveList)
{
superList.Remove(item);
}
Console.WriteLine(superList.Count());
Console.ReadKey();
}
运行截图如下: