如何在 C# 中删除字符串列表中的重复值
本文关键字:列表 删除 字符串 | 更新日期: 2023-09-27 18:36:58
我使用了以下代码:
List<string> lists=new List<string>("apple","orange","banana","apple","mang0","orange");
string names;
names=lists.Distinct()
这是对的吗?
不,变量names
必须是集合。Distinct
方法返回一个枚举器,因此您可能希望枚举结果并将其实现为列表:
List<string> names = lists.Distinct().ToList();
您可以对列表进行排序,然后检查两个和两个:
list.Sort();
Int32 index = 0;
while (index < list.Count - 1)
{
if (list[index] == list[index + 1])
list.RemoveAt(index);
else
index++;
}