[英]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--;
}
筆記:
我喜歡使用這個命令:
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
重命名為您的對象的名稱。
怎么運行的
Union 命令列出兩個源對象的每個條目之一。 由於 obj1 都是源對象,因此這將 obj1 簡化為每個條目之一。
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);
}
如果您有兩個類別的Product
和Customer
並且我們想從他們的列表中刪除重復的項目
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.