Im trying to compare two lists, to find out how which items occur in both lists. I also want to test for duplication within a list.
Now, the obvious way is a loop/compare which works fine for a low number of items. Beyond 10000 items, things slow down much more than a factor of 10. Is there a better way?
Code:Dim List1 As New List(Of String) Dim List2 As New List(Of String) Dim List3 As New List(Of String) Dim List4 As New List(Of String) 'Make two lists of 100000 items For i As Integer = 0 To 100000 - 1 List1.Add("A" & i.ToString) List2.Add("B" & i.ToString) Next 'Add 100 items known to be in List1 to List2 'Add 100 duplicates to List1 For i As Integer = 0 To 100 - 1 List1.Add("A" & i.ToString) List2.Add("A" & i.ToString) Next Dim start As DateTime = Date.Now For Each s As String In List1 If List2.IndexOf(s) > -1 Then List3.Add(s) Next Dim duration As TimeSpan = Date.Now - start MessageBox.Show(List3.Count & " common items found in " & duration.TotalMilliseconds & "ms") start = Date.Now For i As Integer = 0 To List1.Count - 1 If i < List1.LastIndexOf(List1.Item(i)) Then List4.Add(List1.Item(i)) Next duration = Date.Now - start MessageBox.Show(List4.Count & " items with duplicates found in " & duration.TotalMilliseconds & "ms")




Reply With Quote