I will assume that the elements of your arrays are Equatable
.
If they are also Hashable
, and if the order of the elements is not important for you, and if (as in your example), all elements are unique, you might want to consider using set algebra rather than the an ordered collection type such as Array
. E.g. using Set
in Swift allows you to use the subtract(_:)
or subtracting(_:)
mutating/non-methods for 1) and 2), and intersection(_:)
/formIntersection(_:)
for 3), which all use O(1) (amortized) lookup for comparing elements between the sets (as compared to e.g. using O(n) contains(_:) of Array (with Equatable
elements) to lookup existance of some specified element).
For additional details, see the language reference for Set
as well as the thread linked to by vadian:
If the elements in each array is not unique, and you want to keep multiples as well as the order between the elements, you could use the Set
representation of one of the arrays while filtering the other one.
E.g., for:
var firstArray = ["a","b","c"]
var secondArray = ["a","d","e"]
A) in O(n)
:
let excludeElements = Set(secondArray) // O(n)
secondArray = secondArray
.filter { !excludeElements.contains($0) } // O(n) due to O(1) (amortized) .contains lookup
B) in O(n)
:
let excludeElements = Set(firstArray) // O(n)
secondArray = secondArray
.filter { !excludeElements.contains($0) } // O(n) due to O(1) (amortized) .contains lookup
C) in O(n)
, using order and duplicates as they occur in firstArray
:
let includeElements = Set(secondArray) // O(n)
let commonElements = firstArray
.filter(includeElements.contains) // O(n) due to O(1) (amortized) .contains lookup
C) in O(n)
, using order and duplicates as they occur in secondArray
:
let includeElements = Set(firstArray) // O(n)
let commonElements = secondArray
.filter(includeElements.contains) // O(n) due to O(1) (amortized) .contains lookup
Performance?
The above only looks at asymptotic time complexity, and doesn't take into account any actual benchmarking. Generally the functional methods such as filter
are slower than just a for
or while
loop, so if performance becomes an issue for your app, you should consider, at that point, performing profiling as well as custom bench-marking possible bottlenecks in your algorithms.
Also, if your arrays are known to be sorted, there are more efficient ways to traverse them and filter out the result. See e.g. the following thread (C language, but the logic is the important part):