swifthigher-order-functionsset-theory

Is there a Swift one-liner to remove an element from an array if present and append it if not?


This is a construct I come across quite a lot. Is there a nice way to one-line it in Swift?

I could just write an extension on Sequence for it, but I feel like there's a "obvious" higher-order-function / set theory technique that is eluding me.

if array.contains(element) {
    array.removeObject(object: element)
}
else {
    array.append(element)
}

I don't think the solution will even necessarily be nicer per se, it's just something I think about every time I have to write this.


Solution

  • I've found the part of Set Theory that was eluding me! The result I want is the Symmetric Difference of the two arrays and this is included in Swift's set:

    var element = Set([1])
    var set = Set([1, 2, 3])
    
    set = set.symmetricDifference(element) //2, 3
    set = set.symmetricDifference(element) //1, 2, 3