如何删除listview中的重复项?

Krä*_*hne 2 c# listview duplicates

好吧,我需要检查我的应用程序列表视图中是否存在重复项目,但是......我不知道如何.

检测到这种情况的方法是检查字段"Tag",如果它们相同,则删除该项目.

Eli*_*ing 5

查找重复项的一种好方法是使用临时哈希集.这为您提供了一种算法(请参阅Rick Sladkeys评论)以检测重复项.例:O(n) O(n log n)

var tags = new HashSet<string>();
var duplicates = new List<Item>();

foreach(Item item in listView.Items)
{
    // HashSet.Add() returns false if it already contains the key.
    if(!tags.Add(item.Tag) 
        duplicates.Add(item);
}

[Remove duplicates here]
Run Code Online (Sandbox Code Playgroud)