Max*_*Max 1 .net vb.net arrays items list
我有一个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
Run Code Online (Sandbox Code Playgroud)
你有没有尝试过 .Distinct()
Dim stringWithChars As String = "AABBCCDDEEFFaabbccddeeff"
Dim res = stringWithChars.Distinct() // ABCDEFabcdef
Run Code Online (Sandbox Code Playgroud)
编辑:既然你没有说你使用哪个框架,我想你可以使用Linq(.NET 3.5 +)