我有一个 List 对象,我想删除重复的项目,但在列表中至少保留一个重复的项目;
我写了这样的东西但是我会优化这个代码以获得更好的性能,有更快的东西吗?
Const chars As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Dim rnd As New Random()
Dim mylist As List(Of String) = Enumerable.Range(1, 100).Select(Function(i) chars(rnd.Next(0, chars.Length)).ToString).ToList
For n As Integer = mylist.Count - 1 To n = 0 Step -1
'remove the item if it's duplicated
'but leave at least one of the duplicated items in the list
If mylist.IndexOf(mylist.Item(n), 0) < n Then
mylist.RemoveAt(n)
End If
Next