Difference and intersection of two arrays containing objects

You could define three functions inBoth, inFirstOnly, and inSecondOnly which all take two lists as arguments, and return a list as can be understood from the function name. The main logic could be put in a common function operation that all three rely on. Here are a few implementations for that operation to choose from, … Read more

Set operations (union, intersection) on Swift array?

Yes, Swift has the Set class. let array1 = [“a”, “b”, “c”] let array2 = [“a”, “b”, “d”] let set1:Set<String> = Set(array1) let set2:Set<String> = Set(array2) Swift 3.0+ can do operations on sets as: firstSet.union(secondSet)// Union of two sets firstSet.intersection(secondSet)// Intersection of two sets firstSet.symmetricDifference(secondSet)// exclusiveOr Swift 2.0 can calculate on array arguments: set1.union(array2) // … Read more