简体   繁体   中英

Compare two arrays with different dimension

I am trying to compare two arrays that always have different dimensions.

eg. arr1 -> {1,2,3} and arr2->{1,2}

I did try and able to get the matching items to new array. But I am expecting to get the unmatched items only

I am expecting to compare both arrays and put only the item '3' to a new array which is in arr1 and not in arr2

eg arr1 -> {1,2,3} and arr2->{1,2} should result a new array with 3 arr1 -> {1,2,3,4} and arr2->{1,2} should result a array with 3,4

Is there any way of doing this for older version of .net framework without using Enumerable.Except

I have done something like this. if we have two arrays called paramOld {"1","2","3"} and paramNew {"2","3"}

If paramOld.Length > paramNew.Length Then
            Dim paramDelete((paramOld.Length - paramNew.Length) - 1) As String


            Dim isFound As Boolean = False
            For i As Int32 = 0 To oldparamLenght - 1
                isFound = False
                For j As Int32 = 0 To newparamLength - 1
                    If paramOld(i) = paramNew(j) Then
                        isFound = True
                        Exit For
                    End If
                Next
                If isFound = False Then
                    paramDelete(i) = paramOld(i)
                End If
            Next
End If

Can you use LINQ? Except() may be right for you.

var arr3 = arr1.Except(arr2);

Edited

If you can't use LINQ here a quick and dirty version of that function:

Public Shared Function Except(a As List(Of Integer), b As List(Of Integer)) As List(Of Integer)
    Dim result As New List(Of Integer)()

    For Each value As Integer In a
        If Not b.Contains(value) Then
            result.Add(value)
        End If
    Next

    For Each value As Integer In b
        If Not a.Contains(value) Then
            result.Add(value)
        End If
    Next

    Return result
End Function

For two sorted arrays of integers with no duplicates, as shown in your examples,

Public Function differences(a() As Integer, b() As Integer) As Integer()

    Dim aLen As Integer = a.Length
    If aLen = 0 Then
        Return b
    End If

    Dim bLen As Integer = b.Length
    If bLen = 0 Then
        Return a
    End If

    Dim diff As New List(Of Integer)

    Dim i As Integer = 0
    Dim j As Integer = 0

    Do
        If a(i) = b(j) Then
            i += 1
            j += 1
        ElseIf a(i) > b(j) Then
            diff.Add(b(j))
            j += 1
        ElseIf a(i) < b(j) Then
            diff.Add(a(i))
            i += 1
        End If
    Loop While i < aLen AndAlso j < bLen

    If i < aLen Then
        For k = i To aLen - 1
            diff.Add(a(k))
        Next
    End If

    If j < bLen Then
        For k = j To bLen - 1
            diff.Add(b(k))
        Next
    End If

    Return diff.ToArray

End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM