Check if 2 arrays are similar

Hi!

I have two arrays

Arr1 = {“1”,“15”,“7”,“36”}
Arr2 = {“1”,“15”,“8”,“14”}

and I want to check each array item if they are similar and output the following:
Arr3 = {“YES”,“YES”,“NO”,“NO”}

“Yes” if the items are the same, “No” if the items are not

Any suggestions on how to do this?
Thank you

1 Like

@RailCay

Do you mean want to check first element with first element only ?

Check per each element, so for the first element of Arr1 to the first element of Arr2 then to the second element of Arr1 to the second element of Arr2 and so on until the fourth element

@RailCay

Ok. Now got it.

First create one new array to write status. And then use nested ForEach loop to iterate two array.

       ForEach item1 in Arr1
            ForEach item2 in Arr2
                 IF item1.Tostring.Equals(item2)
                  Then use Add to Collection activity and pass "YES" and mention array name as Arr3
                   Else use Add to Collection activity and pass "NO" and mention array name as Arr3

Now Arr3 contains all statuses.

5 Likes

@RailCay,

Welcome to UiPath Community!.

If you need to compare the two arrays sequentially and does not require the results in a third array, then you can use SequenceEqual of String array methods.

If you need to ignore the cases then pass this parameter as well,

strFirst.SequenceEqual(strSecond, StringComparer.OrdinalIgnoreCase)

2 Likes

@lakshman

I got a different answer from what I was expecting

I believe I looped Arr2 inside Arr1.

Where did I make a mistake?

Try this



@RailCay,

Compare2Arrays.xaml (9.7 KB)

Check this xaml, it may help you.

1 Like

@sarathi125

can you explain what condition you entered?:slightly_smiling_face:

@RailCay,

item.Equals(arrSecond(Array.IndexOf(arrFirst, item)), StringComparison.InvariantCultureIgnoreCase)

item - Current Item from my arrFirst inside the loop
arrSecond - Second Array

Since I need to compare the 1st element from first array with the 1st element in the second array
I have used this,

arrSecond(Array.IndexOf(arrFirst, item))

Getting arrSecond array 1 index item using “Array.IndexOf” method
so here in arrSecond(0) item I need to take, so I am getting the item index value from first array.

StringComparison.InvariantCultureIgnoreCase - this is to ignore the case sentive string comparision

3 Likes

@sarathi125

Why does Arr3 create an additional element?

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.