Compare 2 arrays and return Non-Macting items

# Thread: Compare 2 arrays and return Non-Macting items

1. Member
Join Date
Dec 1969
Posts
81

## Compare 2 arrays and return Non-Macting items

Hi all,<BR>I have 2 arrays (arOne, arTwo) and I want to compare the to arrays and put any non-matching items in to a third array (arThree)<BR>For example:<BR><BR>arOne(0)=red<BR>arOne(1)=green<BR> arOne(2)=blue<BR>arOne(3)=orange<BR><BR>arTwo(0)=g reen<BR>arTwo(1)=red<BR>arTwo(2)=blue<BR>arTwo(3)= purple<BR><BR>then array three would be<BR><BR>arThree(0)=purple<BR>arThree(1)=Orange<B R><BR>Im struggleing with the logic. any ideas?<BR><BR>

2. Senior Member
Join Date
Dec 1969
Posts
11,334

## RE: Compare 2 arrays and return Non-Macting items

for i = 0 to ubound(arOne)<BR> for j = 0 to ubound(arTwo)<BR> if arOne(i) = arTwo(j) Then<BR> &#039;I&#039;ll leave you to figure this out<BR> end if<BR> next<BR>next

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## Same answer as last night...

Easy to do if the arrays are sorted. <BR><BR>If they aren&#039;t then use a dictionary object.<BR><BR>Put all the items from arOne into the dictionary.<BR><BR>Then run through arTwo, removing items from the dictionary if they already exist, adding them if they don&#039;t.<BR><BR>When you are done, the only items left in the dictionary will be those in only one of the arrays.<BR><BR>Of course, if the arrays have under (say) 10 or 20 elements each, then maybe a nested loop wouldn&#039;t be *TOO* bad.<BR><BR>

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## And if 1000 elements in each???

You could come back after lunch to see the answer?<BR><BR>I wouldn&#039;t go this route for more than 10 to 20 elements in each array. 100 at the tops.<BR><BR>Same reason I wouldn&#039;t use a bubble sort for more than a handful of elements.<BR><BR>N-Squared algorithms suck.<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
11,334

## RE: And if 1000 elements in each???

Eh, wouldn&#039;t be that bad I wouldn&#039;t think. Sometimes brute force is just easier (in fairness, I have this implemented in C++ code, and million plus loops execute in well under a second, so....)<BR><BR>And it would be infinitely more efficient if the arrays were sorted beforehand. But then again, I didn&#039;t know he reposted this, so I was just throwing things out there.

6. Member
Join Date
Dec 1969
Posts
81

## RE: Compare 2 arrays and return Non-Macting items

ya, I got that far. but that only works if they are in the same order

7. Senior Member
Join Date
Dec 1969
Posts
11,334

## And under a second in VBS

as well... just tested.<BR><BR>1.8Ghz<BR>512MB ram

8. Member
Join Date
Dec 1969
Posts
81

## RE: Same answer as last night...

Ok. thinks bill

9. Senior Member
Join Date
Dec 1969
Posts
96,118

## Huh??? Not true!

Maybe you missed that he used nested loops?<BR><BR>But it takes a lot more "trickery" than what he showed.<BR><BR>Somehow, you have to mark the ones that match. And then, at the end, you have to go back and collect only the unmatched ones.<BR><BR>for i = 0 to ubound(arOne) <BR> for j = 0 to ubound(arTwo) <BR> if Not isNull(arTwo(j)) Then<BR> if arOne(i) = arTwo(j) Then <BR> arTwo(j) = Null &#039; remove both as being duplicates<BR> arOne(i) = Null<BR> Exit For<BR> End If<BR> end if <BR> next <BR>next <BR><BR>And then, when done, you loop through each array separately grabbing the non-Null ones and putting them into the final array.<BR><BR>A royal pain in the butt compared to the Dictionary, isn&#039;t it?<BR><BR>OH! Just realized!<BR><BR>NEITHER of these methods work if there are duplicates *WITHIN* a given array! (Though it&#039;s easy to adapt the dictionary version to fix this!)<BR><BR>

10. Senior Member
Join Date
Dec 1969
Posts
96,118

## That's a lifetime...

...on a busy server.<BR><BR>But admittedly a damned short lunch.<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•