有没有快速去重C#中通用List的方法?
如果您正在使用 .Net 3+,则可以使用 Linq。
List<T> withDupes = LoadSomeData();
List<T> noDupes = withDupes.Distinct().ToList();
也许你应该考虑使用HashSet。
根据MSDN链接:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
HashSet<int> evenNumbers = new HashSet<int>();
HashSet<int> oddNumbers = new HashSet<int>();
for (int i = 0; i < 5; i++)
{
// Populate numbers with just even numbers.
evenNumbers.Add(i * 2);
// Populate oddNumbers with just odd numbers.
oddNumbers.Add((i * 2) + 1);
}
Console.Write("evenNumbers contains {0} elements: ", evenNumbers.Count);
DisplaySet(evenNumbers);
Console.Write("oddNumbers contains {0} elements: ", oddNumbers.Count);
DisplaySet(oddNumbers);
// Create a new HashSet populated with even numbers.
HashSet<int> numbers = new HashSet<int>(evenNumbers);
Console.WriteLine("numbers UnionWith oddNumbers...");
numbers.UnionWith(oddNumbers);
Console.Write("numbers contains {0} elements: ", numbers.Count);
DisplaySet(numbers);
}
private static void DisplaySet(HashSet<int> set)
{
Console.Write("{");
foreach (int i in set)
{
Console.Write(" {0}", i);
}
Console.WriteLine(" }");
}
}
/* This example produces output similar to the following:
* evenNumbers contains 5 elements: { 0 2 4 6 8 }
* oddNumbers contains 5 elements: { 1 3 5 7 9 }
* numbers UnionWith oddNumbers...
* numbers contains 10 elements: { 0 2 4 6 8 1 3 5 7 9 }
*/
HashSet
没有索引,因此不能始终使用它。我必须创建一个没有重复项的大型列表,然后在虚拟模式下将其用于 ListView
。首先创建 HashSet<>
然后转换为 List<>
(这样 ListView
可以通过索引访问项目)是超级快的。List<>.Contains()
的速度太慢了。 - Sinatr怎么样:
var noDupes = list.Distinct().ToList();
在 .net 3.5 中?
只需使用与列表相同类型的HashSet初始化:
var noDupes = new HashSet<T>(withDupes);
或者,如果您想返回一个列表:
var noDupsList = new HashSet<T>(withDupes).ToList();
List<T>
作为结果,请使用 new HashSet<T>(withDupes).ToList()
。 - Tim Schmelter将其排序,然后检查相邻的两个元素是否重复,因为重复的元素会聚集在一起。
类似于这样:
list.Sort();
Int32 index = list.Count - 1;
while (index > 0)
{
if (list[index] == list[index - 1])
{
if (index < list.Count - 1)
(list[index], list[list.Count - 1]) = (list[list.Count - 1], list[index]);
list.RemoveAt(list.Count - 1);
index--;
}
else
index--;
}
注:
我喜欢使用这个命令:
List<Store> myStoreList = Service.GetStoreListbyProvince(provinceId)
.GroupBy(s => s.City)
.Select(grp => grp.FirstOrDefault())
.OrderBy(s => s.City)
.ToList();
我在列表中有这些字段:Id、StoreName、City、PostalCode。我想在下拉菜单中显示具有重复值的城市列表。 解决方案:按城市分组,然后选择第一个放入列表。
对我有效。只需使用
List<Type> liIDs = liIDs.Distinct().ToList<Type>();
用您想要的类型(例如int)替换“Type”。
如kronoz在 .Net 3.5 中所说,您可以使用 Distinct()
。
在 .Net 2 中,您可以模拟它:
public IEnumerable<T> DedupCollection<T> (IEnumerable<T> input)
{
var passedValues = new HashSet<T>();
// Relatively simple dupe check alg used as example
foreach(T item in input)
if(passedValues.Add(item)) // True if item is new
yield return item;
}
这可以用于去重任何集合,并按照原始顺序返回值。
通常情况下,过滤集合(像Distinct()
和这个示例一样)比从中删除项要快得多。
HashSet
构造函数进行了去重,这使得它在大多数情况下更好。然而,这将保留排序顺序,而HashSet
则不会。 - KeithDictionary<T, object>
替换 .Contains
为 .ContainsKey
,.Add(item)
替换为 .Add(item, null)
。 - KeithHashSet
保留顺序,而 Distinct()
则不保留。 - Dennis T --Reinstate Monica--HashSet
有时会根据使用的键类型和输入的相对顺序而发生变化。DedupCollection
代码片段将按照它们输入的顺序返回结果。 - Keith扩展方法可能是一个不错的选择... 就像这样:
public static List<T> Deduplicate<T>(this List<T> listToDeduplicate)
{
return listToDeduplicate.Distinct().ToList();
}
然后可以像这样调用,例如:
List<int> myFilteredList = unfilteredList.Deduplicate();
在Java中(我假设C#是大致相同的):
list = new ArrayList<T>(new HashSet<T>(list))
如果您确实希望改变原始列表:
List<T> noDupes = new ArrayList<T>(new HashSet<T>(list));
list.clear();
list.addAll(noDupes);
为了保持顺序,只需将 HashSet 替换为 LinkedHashSet。
var noDupes = new HashSet<T>(list); list.Clear(); list.AddRange(noDupes);
:) - nawfal
ICollection<MyClass> withoutDuplicates = new HashSet<MyClass>(inputList);
该代码使用哈希集合(HashSet)来去除输入列表(inputList)中的重复项,并将结果保存在一个不包含重复项的MyClass对象集合(withoutDuplicates)中。 - Harald Coppoolse