Find the symmetric difference between two sets in Kotlin

Viewed 1360

Is there a Kotlin stdlib function to find the symmetric difference between two sets? So given two sets [1, 2, 3] and [1, 3, 5] the symmetric difference would be [2, 5].

I've written this extension function which works fine, but it feels like an operation that should already exist within the collections framework.

fun <T> Set<T>.symmetricDifference(other: Set<T>): Set<T> {
    val mine = this subtract other
    val theirs = other subtract this
    return mine union theirs
}

EDIT: What is the best way get the symmetric difference between two sets in java? suggests Guava or ApacheCommons, but I'm wondering if Kotlin's stdlib supports this.

0 Answers
Related