This article explores different ways to get the difference between the two lists in Kotlin. The solution should return all elements that are present in one list but not there in another list.

1. Using minus() function

A simple solution is to use the minus() function to get a list containing all elements of the original collection except the elements contained in the given collection.

Download Code

 
You can further convert the list into a HashSet to speed up the operation.

Download Code

2. Using filterNot() function

Another approach is to use the filterNot() function to get a list containing all elements which are not present in the other list. This function is demonstrated below:

Download Code

3. Using filter() function

Alternatively, you can use the filter() function to achieve the same results.

Download Code

That’s all about getting the difference between the two lists in kotlin.