1. Overview
In this tutorial, we’ll learn how to check if all elements in a Scala List are equal, using only the standard library.
2. Using a Loop
The first approach that we’ll cover is by using a naive solution: a simple loop.
The idea is that we choose an element (e.g., the first) and then check that all other elements are equal to it:
scala> def checkAllEqual(l: List[Int]): Boolean = {
| val base = l.head
| var allEqual = true
| for (elem <- l) {
| if (base != elem) {
| allEqual = false
| }
| }
| allEqual
| }
Now we can test our solution:
scala> checkAllEqual(List(1,1,1,1,1))
res0: Boolean = true
scala> checkAllEqual(List(1,1,1,2,1))
res1: Boolean = false
If we have a very big List, we can improve our solution by adding an early return when it finds a different element. This will make our solution more efficient in many cases:
scala> def checkAllEqual(l: List[Int]): Boolean = {
| val base = l.head
|
| for(elem <- l) {
| if (base != elem) {
| return false
| }
| }
|
| return true
| }
checkAllEqual: (l: List[Int])Boolean
scala> checkAllEqual(List(1,1,1,1,1))
res4: Boolean = true
scala> checkAllEqual(List(1,1,1,2,1))
res5: Boolean = false
As we can see, we get the same output after changing the condition to have an early return. This improvement is not very visible in our example. Still, for bigger lists, it may be the difference between iterating through all elements or finishing after seeing a couple of elements only.
3. Using List.distinct()
Another shorter approach is to use some of the existing methods in the Scala standard lib. The first method we’ll see is the List.distinct() method. This method removes all duplicated elements of the List. We can then check how many unique elements exist by looking at the List size:
scala> List(1,1,1,1).distinct
res1: List[Int] = List(1)
scala> List(1,1,1,1).distinct.size
res2: Int = 1
scala> List(1,1,1,2,1).distinct
res3: List[Int] = List(1, 2)
scala> List(1,1,1,2,1).distinct.size
res4: Int = 2
In these examples, for the case where the List elements are not all equals, the List.distinct().size will return a number bigger than 1. This tells us that not all elements of the original List were equal.
4. Converting the List Into a Set
We also have a different, yet similar, approach. This time, we’ll convert our List into a Set. Sets, by definition, do not contain duplicated elements (except for a few special cases of Sets). If we then convert our original List into a Set, we can inspect the size of the Set to understand if the elements of the original List were all equal, just like we did with the List.distinct() approach:
scala> List(1,1,1,1).toSet
res11: scala.collection.immutable.Set[Int] = Set(1)
scala> List(1,1,1,1).toSet.size
res12: Int = 1
scala> List(1,1,1,2,1).toSet
res13: scala.collection.immutable.Set[Int] = Set(1, 2)
scala> List(1,1,1,2,1).toSet.size
res14: Int = 2
Once again, by looking at the size of the obtained Set, we can determine whether the original List had all elements equal.
5. Using the List.forAll() Method
We’ll now look into a different approach that does not require converting between different collection types. The List.forAll() method can be used to check a given condition for all List elements:
scala> val lst = List(1,1,1,1,1)
lst: List[Int] = List(1, 1, 1, 1, 1)
scala> lst.forall(_ == lst.head)
res1: Boolean = true
scala> val lst = List(1,1,1,2,1)
lst: List[Int] = List(1, 1, 1, 2, 1)
scala> lst.forall(_ == lst.head)
res12: Boolean = false
The idea here is to compare all elements with the head of the List. This method will return true if all List elements pass our condition, meaning that all elements are equal.
6. Using the List.exists() Method
A similar approach is to use another standard lib method; in this case, the List.exists(). Just like the previous one, the method receives a predicate that will evaluate against all elements of the List. This method will return true if there exists an element such that the predicate is matched:
scala> val lst = List(1,1,1,1,1)
lst: List[Int] = List(1, 1, 1, 1, 1)
scala> lst.exists(_ != lst.head)
res18: Boolean = false
scala> val lst = List(1,1,1,2,1)
lst: List[Int] = List(1, 1, 1, 2, 1)
scala> lst.exists(_ != lst.head)
res19: Boolean = true
We used the List.exists() method to look for an element that is different from the List head. When it exists, it returns true. If no element exists, it returns false.
7. Conclusion
In this article, we learned how to check if all elements of a List are equal only by using the Scala standard lib. We started by seeing a very naive approach that requires looping through all elements and then moved into cleaner methods that check the number of distinct elements. Finally, we saw how to make use of the List.forall() and List.exists() methods, where we pass a condition to be evaluated against all elements, allowing us to check if there are any different elements in the List.