繁体   English   中英

从列表中删除重复项<t>在 C#</t>

[英]Remove duplicates from a List<T> in C#

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

如果您使用的是 .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 }
 */

怎么样:

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

在 .net 3.5 中?

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

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

或者,如果您希望返回一个 List:

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

排序,然后检查两个和两个彼此相邻,因为重复项会聚集在一起。

像这样的东西:

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--;
}

笔记:

  • 比较是从后到前进行的,以避免每次删除后都必须重新列出
  • 此示例现在使用 C# 值元组进行交换,如果您不能使用,请替换为适当的代码
  • 最终结果不再排序

我喜欢使用这个命令:

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。

正如 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()和这个示例一样)比从中删除项目要快得多。

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

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。

这需要不同的(没有重复元素的元素)并将其再次转换为列表:

List<type> myNoneDuplicateValue = listValueWithDuplicate.Distinct().ToList();

使用 Linq 的联合方法。

注意:这个解决方案不需要 Linq 的知识,除了它存在之外。

代码

首先将以下内容添加到类文件的顶部:

using System.Linq;

现在,您可以使用以下命令从名为obj1的对象中删除重复项:

obj1 = obj1.Union(obj1).ToList();

注意:将obj1重命名为您的对象的名称。

怎么运行的

  1. Union 命令列出两个源对象的每个条目之一。 由于 obj1 都是源对象,因此这将 obj1 简化为每个条目之一。

  2. ToList()返回一个新列表。 这是必要的,因为像Union这样的 Linq 命令将结果作为 IEnumerable 结果返回,而不是修改原始列表或返回新列表。

作为辅助方法(没有 Linq):

public static List<T> Distinct<T>(this List<T> list)
{
    return (new HashSet<T>(list)).ToList();
}

通过 Nuget 安装MoreLINQ包,您可以通过属性轻松区分对象列表

IEnumerable<Catalogue> distinctCatalogues = catalogues.DistinctBy(c => c.CatalogueCode); 

如果您不关心订单,您可以将项目推入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)空间,所以不是这样我很清楚(乍一看)排序方式较差(我对临时否决票表示歉意......)

这是一种用于原位删除相邻重复项的扩展方法。 首先调用 Sort() 并传入相同的 IComparer。 这应该比重复调用 RemoveAt 的 Lasse V. Karlsen 版本(导致多次块内存移动)更有效。

public static void RemoveAdjacentDuplicates<T>(this List<T> List, IComparer<T> Comparer)
{
    int NumUnique = 0;
    for (int i = 0; i < List.Count; i++)
        if ((i == 0) || (Comparer.Compare(List[NumUnique - 1], List[i]) != 0))
            List[NumUnique++] = List[i];
    List.RemoveRange(NumUnique, List.Count - NumUnique);
}

如果您有两个类别的ProductCustomer并且我们想从他们的列表中删除重复的项目

public class Product
{
    public int Id { get; set; }
    public string ProductName { get; set; }
}

public class Customer
{
    public int Id { get; set; }
    public string CustomerName { get; set; }

}

您必须以下面的形式定义一个泛型类

public class ItemEqualityComparer<T> : IEqualityComparer<T> where T : class
{
    private readonly PropertyInfo _propertyInfo;

    public ItemEqualityComparer(string keyItem)
    {
        _propertyInfo = typeof(T).GetProperty(keyItem, BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public);
    }

    public bool Equals(T x, T y)
    {
        var xValue = _propertyInfo?.GetValue(x, null);
        var yValue = _propertyInfo?.GetValue(y, null);
        return xValue != null && yValue != null && xValue.Equals(yValue);
    }

    public int GetHashCode(T obj)
    {
        var propertyValue = _propertyInfo.GetValue(obj, null);
        return propertyValue == null ? 0 : propertyValue.GetHashCode();
    }
}

然后,您可以删除列表中的重复项。

var products = new List<Product>
            {
                new Product{ProductName = "product 1" ,Id = 1,},
                new Product{ProductName = "product 2" ,Id = 2,},
                new Product{ProductName = "product 2" ,Id = 4,},
                new Product{ProductName = "product 2" ,Id = 4,},
            };
var productList = products.Distinct(new ItemEqualityComparer<Product>(nameof(Product.Id))).ToList();

var customers = new List<Customer>
            {
                new Customer{CustomerName = "Customer 1" ,Id = 5,},
                new Customer{CustomerName = "Customer 2" ,Id = 5,},
                new Customer{CustomerName = "Customer 2" ,Id = 5,},
                new Customer{CustomerName = "Customer 2" ,Id = 5,},
            };
var customerList = customers.Distinct(new ItemEqualityComparer<Customer>(nameof(Customer.Id))).ToList();

此代码通过Id删除重复项如果您想通过其他属性删除重复项,您可以更改nameof(YourClass.DuplicateProperty) same nameof(Customer.CustomerName)然后通过CustomerName属性删除重复项。

简单地确保不将重复项添加到列表中可能更容易。

if(items.IndexOf(new_item) < 0) 
    items.add(new_item)

你可以使用联盟

obj2 = obj1.Union(obj1).ToList();

David J. 的答案是一个很好的方法,不需要额外的对象、排序等。但是可以改进:

for (int innerIndex = items.Count - 1; innerIndex > outerIndex ; innerIndex--)

因此,外循环在整个列表中从上到下,但内循环在“到达外循环位置之前”在底部。

外循环确保处理整个列表,内循环找到实际的重复项,这些只能发生在外循环尚未处理的部分。

或者,如果您不想为内循环执行自下而上的操作,则可以让内循环从 externalIndex + 1 开始。

.Net 2.0 中的另一种方式

    static void Main(string[] args)
    {
        List<string> alpha = new List<string>();

        for(char a = 'a'; a <= 'd'; a++)
        {
            alpha.Add(a.ToString());
            alpha.Add(a.ToString());
        }

        Console.WriteLine("Data :");
        alpha.ForEach(delegate(string t) { Console.WriteLine(t); });

        alpha.ForEach(delegate (string v)
                          {
                              if (alpha.FindAll(delegate(string t) { return t == v; }).Count > 1)
                                  alpha.Remove(v);
                          });

        Console.WriteLine("Unique Result :");
        alpha.ForEach(delegate(string t) { Console.WriteLine(t);});
        Console.ReadKey();
    }

一个简单直观的实现:

public static List<PointF> RemoveDuplicates(List<PointF> listPoints)
{
    List<PointF> result = new List<PointF>();

    for (int i = 0; i < listPoints.Count; i++)
    {
        if (!result.Contains(listPoints[i]))
            result.Add(listPoints[i]);
        }

        return result;
    }

有很多方法可以解决 - 列表中的重复问题,以下是其中之一:

List<Container> containerList = LoadContainer();//Assume it has duplicates
List<Container> filteredList = new  List<Container>();
foreach (var container in containerList)
{ 
  Container duplicateContainer = containerList.Find(delegate(Container checkContainer)
  { return (checkContainer.UniqueId == container.UniqueId); });
   //Assume 'UniqueId' is the property of the Container class on which u r making a search

    if(!containerList.Contains(duplicateContainer) //Add object when not found in the new class object
      {
        filteredList.Add(container);
       }
  }

干杯 Ravi Ganesan

所有答案都复制列表,或创建一个新列表,或使用缓慢的功能,或者只是非常缓慢。

据我所知,这是我所知道的最快和最便宜的方法(而且,由一位专门从事实时物理优化的非常有经验的程序员提供支持)。

// Duplicates will be noticed after a sort O(nLogn)
list.Sort();

// Store the current and last items. Current item declaration is not really needed, and probably optimized by the compiler, but in case it's not...
int lastItem = -1;
int currItem = -1;

int size = list.Count;

// Store the index pointing to the last item we want to keep in the list
int last = size - 1;

// Travel the items from last to first O(n)
for (int i = last; i >= 0; --i)
{
    currItem = list[i];

    // If this item was the same as the previous one, we don't want it
    if (currItem == lastItem)
    {
        // Overwrite last in current place. It is a swap but we don't need the last
       list[i] = list[last];

        // Reduce the last index, we don't want that one anymore
        last--;
    }

    // A new item, we store it and continue
    else
        lastItem = currItem;
}

// We now have an unsorted list with the duplicates at the end.

// Remove the last items just once
list.RemoveRange(last + 1, size - last - 1);

// Sort again O(n logn)
list.Sort();

最终成本为:

nlogn + n + nlogn = n + 2nlogn = O(nlogn)这很好。

关于 RemoveRange 的注意事项:由于我们无法设置列表的计数并避免使用 Remove 函数,我不知道此操作的确切速度,但我想这是最快的方法。

这是一个简单的解决方案,不需要任何难以阅读的 LINQ 或任何事先对列表进行排序。

   private static void CheckForDuplicateItems(List<string> items)
    {
        if (items == null ||
            items.Count == 0)
            return;

        for (int outerIndex = 0; outerIndex < items.Count; outerIndex++)
        {
            for (int innerIndex = 0; innerIndex < items.Count; innerIndex++)
            {
                if (innerIndex == outerIndex) continue;
                if (items[outerIndex].Equals(items[innerIndex]))
                {
                    // Duplicate Found
                }
            }
        }
    }
  public static void RemoveDuplicates<T>(IList<T> list )
  {
     if (list == null)
     {
        return;
     }
     int i = 1;
     while(i<list.Count)
     {
        int j = 0;
        bool remove = false;
        while (j < i && !remove)
        {
           if (list[i].Equals(list[j]))
           {
              remove = true;
           }
           j++;
        }
        if (remove)
        {
           list.RemoveAt(i);
        }
        else
        {
           i++;
        }
     }  
  }

如果需要比较复杂的对象,则需要在 Distinct() 方法中传递一个 Comparer 对象。

private void GetDistinctItemList(List<MyListItem> _listWithDuplicates)
{
    //It might be a good idea to create MyListItemComparer 
    //elsewhere and cache it for performance.
    List<MyListItem> _listWithoutDuplicates = _listWithDuplicates.Distinct(new MyListItemComparer()).ToList();
        
    //Choose the line below instead, if you have a situation where there is a chance to change the list while Distinct() is running.
    //ToArray() is used to solve "Collection was modified; enumeration operation may not execute" error.
    //List<MyListItem> _listWithoutDuplicates = _listWithDuplicates.ToArray().Distinct(new MyListItemComparer()).ToList();

    return _listWithoutDuplicates;
}

假设您还有 2 个其他类,例如:

public class MyListItemComparer : IEqualityComparer<MyListItem>
{
    public bool Equals(MyListItem x, MyListItem y)
    {
        return x != null 
               && y != null 
               && x.A == y.A 
               && x.B.Equals(y.B); 
               && x.C.ToString().Equals(y.C.ToString());
    }

    public int GetHashCode(MyListItem codeh)
    {
        return codeh.GetHashCode();
    }
}

和:

public class MyListItem
{
    public int A { get; }
    public string B { get; }
    public MyEnum C { get; }

    public MyListItem(int a, string b, MyEnum c)
    {
        A = a;
        B = b;
        C = c;
    }
}

我认为最简单的方法是:

创建一个新列表并添加唯一项。

例子:

        class MyList{
    int id;
    string date;
    string email;
    }
    
    List<MyList> ml = new Mylist();

ml.Add(new MyList(){
id = 1;
date = "2020/09/06";
email = "zarezadeh@gmailcom"
});

ml.Add(new MyList(){
id = 2;
date = "2020/09/01";
email = "zarezadeh@gmailcom"
});

 List<MyList> New_ml = new Mylist();

foreach (var item in ml)
                {
                    if (New_ml.Where(w => w.email == item.email).SingleOrDefault() == null)
                    {
                        New_ml.Add(new MyList()
                        {
                          id = item.id,
     date = item.date,
               email = item.email
                        });
                    }
                }

使用HashSet这可以很容易地完成。

List<int> listWithDuplicates = new List<int> { 1, 2, 1, 2, 3, 4, 5 };
HashSet<int> hashWithoutDuplicates = new HashSet<int> ( listWithDuplicates );
List<int> listWithoutDuplicates = hashWithoutDuplicates.ToList();

根据删除重复项,我们必须应用以下逻辑,以便快速删除重复项。

public class Program
{

    public static void Main(string[] arges)
    {
        List<string> cities = new List<string>() { "Chennai", "Kolkata", "Mumbai", "Mumbai","Chennai", "Delhi", "Delhi", "Delhi", "Chennai", "Kolkata", "Mumbai", "Chennai" };
        cities = RemoveDuplicate(cities);

        foreach (var city in cities)
        {
            Console.WriteLine(city);
        }
    }

    public static List<string> RemoveDuplicate(List<string> cities)
    {
        if (cities.Count < 2)
        {
            return cities;
        }

        int size = cities.Count;
        for (int i = 0; i < size; i++)
        {
            for (int j = i+1; j < size; j++)
            {
                if (cities[i] == cities[j])
                {
                    cities.RemoveAt(j);
                    size--;
                    j--;
                }
            }
        }
        return cities;
    }
}

使用 HashSet: list = new HashSet<T>(list).ToList();

YourList.GroupBy(i => i.Id).Select(i => i.FirstOrDefault()).ToList();

我有我自己的方式。 我是 2 循环比较列表项的相同列表。 然后删除第二个。

            for(int i1 = 0; i1 < lastValues.Count; i1++)
            {
                for(int i2 = 0; i2 < lastValues.Count; i2++)
                {
                    if(lastValues[i1].UserId == lastValues[i2].UserId)
                    {
                        lastValues.RemoveAt(i2);
                    }
                }
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM