# BBC BASIC Programmers' Reference

### Site Tools

comparing_20arrays_20for_20equality

## Comparing arrays for equality

by Richard Russell, November 2012

Supposing you have two numeric arrays (of the same size) and you want to test whether they are identical, i.e. that their contents are the same. Obviously you could iterate through the indices, comparing each element in turn with the corresponding element in the other array, but that's a bit messy. Luckily there is a much neater way to do it:

```        array1() -= array2()
IF MOD(array1()) = 0 THEN
PRINT "Arrays are identical"
ELSE
PRINT "Arrays are different"
ENDIF```

This works because the MOD(array()) function returns zero only if all the elements of the array are zero.

Of course the above test is destructive of the contents of array1(), which may not be desirable. If you want to preserve the contents of the array one method is to reverse the effect of the subtraction by adding back array2():

```        array1() -= array2()
equal% = MOD(array1()) = 0
array1() += array2()```

Here equal% is set TRUE if the arrays are identical and FALSE otherwise.

Another way of preserving the contents of array1() is to use a third array (with the same dimensions as the other arrays):

```        test() = array1() - array2()
equal% = MOD(test()) = 0``` 