This post will check if two integer arrays are equal or not in Kotlin. Two arrays are considered equal if both arrays contain the same elements in the same order.

1. Using contentEquals() function

To compare two arrays for equality in Kotlin, you can use the array comparison functions contentEquals() and contentDeepEquals(). For a single-dimensional array, use the contentEquals() function:

Download Code

Output:

Both arrays are not equal

 
For multidimensional arrays, use the contentDeepEquals() function, since contentEquals() doesn’t work.

Download Code

Output:

contentEquals() returns false
contentDeepEquals() returns true

2. Using Custom Routine

You can even write your own logic for checking array equality. Here’s what the code would look like for single dimensional arrays:

Download Code

Output:

Both arrays are equal

 
Similar to the single-dimensional arrays, you can write your utility function to check array equality for 2-dimensional arrays.

Download Code

Output:

Both arrays are equal

That’s all about comparing two integer arrays in Kotlin.