## 1. Introduction

In programming, dealing with lists and comparing elements in a list is a common task.

In this tutorial, we’ll explore various approaches to determining if all elements in a List are the same.

## 2. Using a for() Loop

First, we can use a classic for() loop to check if all elements of a list are the same:

``````fun areAllElementsSameUsingForLoop(list: List<Int>): Boolean {
if (list.isEmpty() || list.size == 1) return true
val firstElement = list[0]
for (element in list) {
if (element != firstElement) {
return false
}
}

return true
}``````

In the code above, we iterate the list using a for() loop and check that all elements are equal to the first element of the list. If we find any mismatch, we return false immediately, otherwise, we return true. Furthermore, an empty list or a list with just one element will always return true.

Let’s ensure this method works as expected:

``````@Test
fun `test all elements are same using for loop`() {
val list1 = listOf(3, 3, 3)
val list2 = listOf(2,3,4)
val list3 = emptyList<Int>()

assertTrue(areAllElementsSameUsingForLoop(list1))
assertFalse(areAllElementsSameUsingForLoop(list2))
assertTrue(areAllElementsSameUsingForLoop(list3))
}``````

## 3. Using the all() Method

Kotlin’s all() method is a built-in function that returns true if all the elements in a given list satisfy a given predicate:

``````fun areAllElementsSameUsingAllMethod(list: List<Int>) = list.all { it == list[0] }
``````

In this code snippet, we’re using the all() method to check if all the elements in a list are the same. We pass in a predicate function that compares each element of the list with the first element. Note that this method returns true if the List or Collection is empty.

Now, let’s test this method to make sure it works correctly:

``````@Test
fun `test all elements are same using all method`() {
val list1 = listOf(3, 3, 3)
val list2 = listOf(2,3,4)
val list3 = emptyList<Int>()

assertTrue(areAllElementsSameUsingAllMethod(list1))
assertFalse(areAllElementsSameUsingAllMethod(list2))
assertTrue(areAllElementsSameUsingAllMethod(list3))
}``````

## 4. Using the distinct() Method

The distinct() method is another built-in method in Kotlin. This returns a list of distinct elements from a List or Collection, and if the collection is empty, this method returns an empty list:

``````fun areAllElementsSameUsingDistinctMethod(list: List<Int>) =
list.distinct().size <= 1
``````

So, to know if all elements of a list are the same, this method should ensure that the number of distinct elements in the list is zero (for an empty input list) or one (for a non-empty input list). Anything above one remaining means there are at least two different elements in the original list, and our method will return false.

Let’s test this method for correctness:

``````@Test
fun `test all elements are same using distinct method`() {
val list1 = listOf(3, 3, 3)
val list2 = listOf(2,3,4)
val list3 = emptyList<Int>()

assertTrue(areAllElementsSameUsingDistinctMethod(list1))
assertFalse(areAllElementsSameUsingDistinctMethod(list2))
assertTrue(areAllElementsSameUsingDistinctMethod(list3))
}``````

## 5. Using the count() Method

Similarly, we can use the count() method to achieve our goal. By using the first element of the list, we can check if the frequency of that element in the list is equal to the size of the list:

``````fun areAllElementsSameUsingCountMethod(list: List<Any>): Boolean {
return list.count { it == list[0] } == list.size
}``````

The count() method accepts a predicate function that compares each element to the first element of the list while counting matches.

Let’s test this method as well:

``````@Test
fun `test all elements are same using count method`() {
val list1 = listOf(3, 3, 3)
val list2 = listOf(2,3,4)
val list3 = emptyList<Int>()

assertTrue(areAllElementsSameUsingCountMethod(list1))
assertFalse(areAllElementsSameUsingCountMethod(list2))
assertTrue(areAllElementsSameUsingCountMethod(list3))
}``````

## 6. Using a Set

Another straightforward way to check if all elements in a list are the same involves using a Set. A set contains distinct elements and does not allow duplicates:

``````fun areAllElementsSameUsingSetMethod(list: List<Any>): Boolean {
return list.toSet().size <= 1
}``````

In this code, we first convert the List to a Set using the toSet() method and then check if the set contains one or fewer elements. Accordingly, if it does, we return true. Conversely, we return false if the Set contains more than one element.

As usual, let’s check that this method works correctly:

``````@Test
fun `test all elements are same using set method`() {
val list1 = listOf(3, 3, 3)
val list2 = listOf(2,3,4)
val list3 = emptyList<Int>()

assertTrue(areAllElementsSameUsingSetMethod(list1))
assertFalse(areAllElementsSameUsingSetMethod(list2))
assertTrue(areAllElementsSameUsingSetMethod(list3))
}``````

## 7. Conclusion

In this article, we’ve explored various ways to check if the elements of a list are the same in Kotlin. We looked at the use of built-in methods to achieve this such as the all(), distinct(), and count() methods. Furthermore, we explored other techniques like the classic for() loop and Set data structure.

As always, the complete source code for the examples is available over on GitHub.
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.