有没有快速去重C#中通用List的方法?
这将取出不重复的元素,并将其再次转换为列表:
List<type> myNoneDuplicateValue = listValueWithDuplicate.Distinct().ToList();
using System.Linq;
obj1
的对象中删除重复项:obj1 = obj1.Union(obj1).ToList();
注意:将obj1
重命名为您的对象的名称。
它是如何工作的
Union命令列出了两个源对象中每个条目的一个。由于 obj1
是两个源对象,因此将其减少到每个条目的一个。
ToList()
返回一个新列表。这是必需的,因为 Linq 命令如 Union
将结果作为 IEnumerable 结果返回,而不是修改原始列表或返回新列表。
public static List<T> Distinct<T>(this List<T> list)
{
return (new HashSet<T>(list)).ToList();
}
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);
}
通过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)
,因此我认为(与一开始看到的不同)排序的方式并不劣于使用HashSet的方式。
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)
更改为nameof(Customer.CustomerName)
,然后通过CustomerName
属性删除重复项。
if(items.IndexOf(new_item) < 0)
items.add(new_item)
List<T>.Contains
方法,但是有超过1,000,000个条目。这个过程会减慢我的应用程序。我首先使用了List<T>.Distinct().ToList<T>()
。 - RPDeshaiespublic 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;
}
您可以使用 Union
obj2 = obj1.Union(obj1).ToList();
ICollection<MyClass> withoutDuplicates = new HashSet<MyClass>(inputList);
该代码使用哈希集合(HashSet)来去除输入列表(inputList)中的重复项,并将结果保存在一个不包含重复项的MyClass对象集合(withoutDuplicates)中。 - Harald Coppoolse