For loop if statement print non exist


#1

Hi,

I have two arrays
arrA: {22222,21530,11111,21994,22009,12345}
arrB: {21530, 21994, 22009}

I want to return 22222, 11111, 12345

so I use
for each a in arrA:
for each b in arrB:
if a.tostring <> b.tostring:
string abc = string.Join(",", abc, a)

However, I am geeting the result:
image

It’s not duplicate issue, it’s wrong result. How can I get only 22222, 11111, 12345?? Thanks!
I am aware of this:


but doesn’t help much.

P.S. prior to this I actually have another nested loop and if statement for validation - I tried to get all these non exist number in the ELSE portion but facing similar issue because it’s a nested loop… that’s why I created the 2nd nested loop hoping to put the numbers in the THEN statement… >.>…

Lavina


#2

The result is because every number in first array is compared with each and every number in the second array that is why it is printing like this!
Try using while loop instead. That’ll give you the correct result!


#3

Hi,

I am really not sure how to use while loop to accomplish this:

since I need to check against each item.
Here is my attempt:

n = 0 by default
and existXlsxClaimArr.length is always <= claimL2.length

Any thoughts?

and I got:
image

The length of this is already wrong. It should return only 3 items - 22222, 11111, 12345

Thanks!
Lavina


#4

Hi! Here is a working worklow I made for you:
Arrays.xaml (10.4 KB)

What I’ve done is that I’ve taken a counter variable which I am incrementing when two elements are unequal…
After the inner for loop I am checking if that counter variable is equal to the length of inner array if yes, then join that particular number!

Hope you get this :smiley:


#5

Hi @Niket_Ghai,

It works well! Thanks a lot!

Warm regards,
Lavina


#6

Hi @lavint,

I know I am late to the party, but check out the alternative solution using .Contains().
It simplifies the entire process by a lot:

CompareAndJoinArrays.xaml (6.8 KB)


#7

Hi @loginerror,

That’s right! Never too late. :slight_smile: It’s definitely more simple this way. Thanks!

Warm regards,
Lavina