Find the symmetric difference between two sets in Kotlin

谁说胖子不能爱 提交于 2021-02-10 12:58:01

问题


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.

来源:https://stackoverflow.com/questions/65361494/find-the-symmetric-difference-between-two-sets-in-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!