This article explores different ways to determine if all values in an array are the same in Kotlin.

1. Using a Set

A simple solution is to convert the given array into a Set and get its size. If the size of the Set is 1, we can say that all values in the array are the same.

Download Code

2. Using distinct() function

The idea here is to get the distinct element count of the array. If the count is 1, all elements in the array must be equal. This would translate to a simple code below:

Download Code

3. Using all() function

Alternatively, we can check if all values are equal to the first element of the array. The cleanest way to do this is using the all() function, as shown below:

Download Code

 
Here’s an equivalent version without using the all() function:

Download Code

That’s all about determining if all values in an array are the same in Kotlin.