Freigeben über


Another way to find duplicates in two strings.

I think the LINQ way is a way more expressive compared to the loops although conceptually they do the same thing.

Function LINQ(ByVal arr1 As String(), ByVal arr2 As String) As String()

Dim duplicates = From s0 In arr1, s1 In arr2 _

Where s0 = s1 _

Select s0

Return duplicates.ToArray()

End Function

Function Loops(ByVal arr1 As String(), ByVal arr2 As String()) As String()

Dim l As New List(Of String)

For i As Integer = 0 To UBound(arr1)

For j As Integer = 0 To UBound(arr2)

If arr1(i) = arr2(j) Then l.Add(arr1(i))

Next

Next

Return l.ToArray()

End Function

.

Comments

  • Anonymous
    April 21, 2006
    Both solutions seems incorrect. You will have doubles — every string will be returned twice.