Compare two arrays for equality in Kotlin
This post will discuss how to determine whether two arrays are equal to each other in Kotlin. Two arrays are considered equal if they contain the same number of same elements in the same order.
1. Single Dimensional Arrays
Kotlin 1.1 introduced extension functions for element-by-element operations on arrays. You can use the contentEquals() function for single dimensional array comparison, which returns true when both arrays are structurally equal.
|
1 2 3 4 5 6 7 |
fun main() { val first = arrayOf(1, 2, 3) val second = arrayOf(1, 2, 3) val isEqual = first.contentEquals(second) if (isEqual) println("Arrays are equal") else println("Arrays are not equal") } |
Output:
Arrays are equal
Since the extension functions are infix, you can use them in the following way:
|
1 2 3 4 5 6 7 |
fun main() { val first = arrayOf(1, 2, 3) val second = arrayOf(1, 2, 3) val isEqual = first contentEquals second if (isEqual) println("Arrays are equal") else println("Arrays are not equal") } |
Output:
Arrays are equal
You can even write your custom logic to check for array equality. We can compare two elements for equality with the equals() function. The algorithm can be implemented as follows in Kotlin.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
fun<T> isEqual(first: Array<T>?, second: Array<T>?): Boolean { if (first == second) { return true } if (first == null || second == null) { return false } if (first.size != second.size) { return false } for (i in first.indices) { if (!first[i]?.equals(second[i])!!) { return false } } return true } fun main() { val first = arrayOf(1, 2, 3) val second = arrayOf(1, 2, 3) val isEqual = isEqual(first, second) if (isEqual) println("Arrays are equal") else println("Arrays are not equal") } |
Output:
Arrays are equal
2. Two-Dimensional Arrays
Similar to the single-dimensional array, you can write our utility function for checking the array equality.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
fun<T> isEqual(first: Array<Array<T>>, second: Array<Array<T>>): Boolean { if (first == second) { return true } if (first.size != second.size) { return false } for (i in first.indices) { if (first[i].size != second[i].size) { return false } for (j in first[i].indices) { if (!first[i][j]?.equals(second[i][j])!!) { return false } } } return true } fun main() { val first = arrayOf(arrayOf(1, 2, 3), arrayOf(4, 5, 6)) val second = arrayOf(arrayOf(1, 2, 3), arrayOf(4, 5, 6)) val isEqual = isEqual(first, second) if (isEqual) println("Arrays are equal") else println("Arrays are not equal") } |
Output:
Arrays are equal
3. Multi-Dimensional Arrays
You can use the contentDeepEquals() function for multidimensional arrays, which returns true if the two specified arrays are deeply equal.
|
1 2 3 4 5 6 7 |
fun main() { val first = arrayOf(arrayOf(1, 2, 3), arrayOf(4, 5, 6)) val second = arrayOf(arrayOf(1, 2, 3), arrayOf(4, 5, 6)) val isEqual = first.contentDeepEquals(second) if (isEqual) println("Arrays are equal") else println("Arrays are not equal") } |
Output:
Arrays are equal
4. Array of Objects
The two object arrays are considered equal if
- Both array references are
nullor both arenon-null. - Both arrays have same type and contain the same number of elements.
- All corresponding pairs of objects in the arrays are equal.
An easy way to compare two arrays of objects is to use the contentEquals() function and have your class override the equals() and hashCode() methods. Alternatively, you can use a data class that automatically derives the equals() and hashCode().
|
1 2 3 4 5 6 7 8 9 |
data class Person (var name: String?, var age: Int?) fun main() { val first = arrayOf(Person("John", 20), Person("Mary", 15)) val second = arrayOf(Person("John", 20), Person("Mary", 15)) val isEqual = first.contentEquals(second) if (isEqual) println("Arrays are equal") else println("Arrays are not equal") } |
Output:
Arrays are equal
That’s all about comparing two arrays for equality in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)