# Compare 2 1D arrays (with a same size) quickly

## Recommended Posts

i would like to ask if there's any way to compare 2 1D arrays quickly rather than using for-next loop to compare them one by one?

thanks.

##### Share on other sites

It depends on the result you need. If you just want to know if they are equal then you could use function _ArrayToString and compare the resulting strings.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2020-10-10 - Version 1.5.2.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2020-06-30 - Version 1.6.2.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

##### Share on other sites

You must compare each element from each array otherwise you can't be sure whether both are equal or not!

If the array is sorted than it is much easier to compare, otherwise you need to sort both arrays and compare it afterwards.

Br,

UEZ

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

##### Share on other sites

What is "compare" ?

Take

```A    A
B    C
C    B```

They are DIFFERENT because the order is not the same.

They are IDENTICAL because the content is the same

my ideas would be

case 1) make a string and compare it

case 2) sort and compare - OR add together values of ascii codes and compare total number. (like: 2+3 = 3+2 = 5)

I am just a hobby programmer, and nothing great to publish right now.

##### Share on other sites

I wrote you a small function.

```Global \$array1[3] = [1,2,3], \$array2[3] = [1,3,2]

If _ArrayCompare(\$array1, \$array2) Then
MsgBox(0, "", "Arrays are identical")
Else
MsgBox(0, "", "Arrays are different")
EndIf

Func _ArrayCompare(\$aArray1, \$aArray2)
If Ubound(\$aArray1, 0) <> 1 Or Ubound(\$aArray2, 0) <> 1 Or Ubound(\$aArray1, 1) <> Ubound(\$aArray2, 1) Then Return SetError (1, 0, False)

Local \$iBound = UBound(\$aArray1)
For \$i = 0 To \$iBound -1
If Not (\$aArray1[\$i] == \$aArray2[\$i]) Then Return SetError(2, 0, False) ; No more comparisons needed.
Next
Return True
EndFunc```

I had a small oversight. - Fixed.

Edited by czardas

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...