从C#中的List <T>中删除重复项

时间:2020-03-05 18:48:46  来源:igfitidea点击:

任何人都有在C#中删除通用列表的快速方法吗?

解决方案

回答

对其进行排序,然后将两个和两个相邻检查,因为重复项会聚集在一起。

像这样的东西:

list.Sort();
Int32 index = 0;
while (index < list.Count - 1)
{
    if (list[index] == list[index + 1])
        list.RemoveAt(index);
    else
        index++;
}

回答

也许我们应该考虑使用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 }
 */

回答

在Java中(我假设Cis大致相同):

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。

回答

如果我们不关心订单,则可以将项目推入" HashSet"中,如果我们希望维持订单,则可以执行以下操作:

var unique = new List<T>();
var hs = new HashSet<T>();
foreach (T t in list)
    if (hs.Add(t))
        unique.Add(t);

或者Linq方式:

var hs = new HashSet<T>();
list.All( x =>  hs.Add(x) );

编辑:HashSet方法是O(N)时间和O(N)空间,同时进行排序然后使其唯一(如@lassevk等建议)是O(N * lgN)时间和O(1)的空间,所以我不太清楚(乍一看)排序方式不如(我临时表示歉意...)

回答

怎么样:-

var noDupes = list.Distinct().ToList();

在.net 3.5中?

回答

如果我们使用的是.Net 3+,则可以使用Linq。

List<T> withDupes = LoadSomeData();
List<T> noDupes = withDupes.Distinct().ToList();

回答

正如克罗诺兹在.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()和本示例一样)。

回答

只需使用相同类型的List初始化HashSet即可:

var noDupes = new HashSet<T>(withDupes);

或者,如果我们希望返回列表:

var noDupsList = new HashSet<T>(withDupes).ToList();

回答

扩展方法可能是一种不错的方法...像这样:

public static List<T> Deduplicate<T>(this List<T> listToDeduplicate)
{
    return listToDeduplicate.Distinct().ToList();
}

然后像这样调用,例如:

List<int> myFilteredList = unfilteredList.Deduplicate();