Random User
Random User

Reputation: 365

Comparing two arrays in vb.net

I need to do Reverse Intersection operation with two arrays and save the result in a different array

Eg: Array A {1, 2, 3}; Array B {1, 2, 3, 4, 5, 6} Resultant Array Should be {4, 5, 6}

I Tried out the following logic but didn't work

int k = 0;
int a[2] = {1,10};
int p[10];
int roll[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

for (int i = 0; i < 2; i++)
{
    for (int j = 1; j <= 10; j++)
    {
        if (a[i] == roll[j])
        {
            break;
        }
        else
        {
            p[k] = 0;
            p[k] = roll[j];
            k++;
        }
    }
}

I need it for my vb.net project

Upvotes: 1

Views: 11514

Answers (2)

SysDragon
SysDragon

Reputation: 9888

Try something like this if you can't use Linq:

Function RevIntersect(arr1() As String, arr2() As String) As String()
    Dim sResult, aux As New List(Of String)()

    aux.AddRange(arr1)
    aux.AddRange(arr2)

    For Each elem As String In aux
        If (Not arr1.Contains(elem) OrElse Not Arr2.Contains(elem)) AndAlso _
        Not sResult.Contains(elem) Then  
            sResult.Add(elem)
        End If
    Next

    Return sResult.ToArray()
End Function

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460138

I don't understand how that C# code is related to your VB.NET problem, if you only want to find integers which are in one array and not in the other, use Enumerable.Except:

Dim intsA = {1, 2, 3}
Dim intsB = {1, 2, 3, 4, 5, 6}
Dim bOnly = intsB.Except(intsA).ToArray()

Upvotes: 9

Related Questions