Compare two integer arrays in Kotlin
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:
|
1 2 3 4 5 6 7 8 9 10 11 |
fun main() { val first = intArrayOf(1, 2, 3) val second = intArrayOf(4, 5, 6) val result = first.contentEquals(second) if (result) { println("Both arrays are equal") } else { println("Both arrays are not equal") } } |
Output:
Both arrays are not equal
For multidimensional arrays, use the contentDeepEquals() function, since contentEquals() doesn’t work.
|
1 2 3 4 5 6 7 |
fun main() { val first = arrayOf(intArrayOf(1, 2, 3), intArrayOf(4, 5, 6)) val second = arrayOf(intArrayOf(1, 2, 3), intArrayOf(4, 5, 6)) println("contentEquals() returns " + first.contentEquals(second)) println("contentDeepEquals() returns " + first.contentDeepEquals(second)) } |
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:
|
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 |
fun isEqual(first: IntArray?, second: IntArray?): Boolean { if (first == null || second == null) { return false } if (first.size != second.size) { return false } for (i in first.indices) { if (first[i] != second[i]) { return false } } return true } fun main() { val first = intArrayOf(1, 2, 3) val second = intArrayOf(1, 2, 3) val result = isEqual(first, second) if (result) { println("Both arrays are equal") } else { println("Both arrays are not equal") } } |
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.
|
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 30 31 |
fun isEqual(first: Array<IntArray>?, second: Array<IntArray>?): Boolean { if (first == null || second == null) { return false } 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] != second[i][j]) { return false } } } return true } fun main() { val first = arrayOf(intArrayOf(1, 2, 3), intArrayOf(4, 5, 6)) val second = arrayOf(intArrayOf(1, 2, 3), intArrayOf(4, 5, 6)) val result = isEqual(first, second) if (result) { System.out.printf("Both arrays are equal") } else { System.out.printf("Both arrays are not equal") } } |
Output:
Both arrays are equal
That’s all about comparing two integer arrays 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 :)