Webb John Webb John - 4 months ago 18
Vb.net Question

Get the unique value based on comparing two arrays

I have two array strings that look like this

array1 = "January","February","September"
array2 = "January","December","September"


Both arrays have the same number of indexes for comparison purposes. What I want to achieve is to display the string "December", since the value of both array indexes did not match each other.

Answer

You can put the values into an array and use Except to find the ones that don't match.

From your description I'm not sure if you want the contents of one that doesn't match the other or all unmatching items, so I have given all options here:

    Dim array1 = {"January", "February", "September"}
    Dim array2 = {"January", "December", "September"}

    'Pick one of the following three options
    Dim inFirstButNotSecond = array1.Except(array2) 'Gives February
    Dim inSecondButNotFirst = array2.Except(array1) 'Gives December
    Dim uniqueVals = inFirstButNotSecond.Concat(inSecondButNotFirst) 'Gives February, December

The beauty of this is that it doesn't matter how many elements are in each array

Comments