iOS swift remove elements of an array from another array

The easiest way is to convert both arrays to sets, subtract the second from the first, convert to the result to an array and assign it back to array1:

array1 = Array(Set(array1).subtracting(array2))

Note that your code is not valid Swift - you can use type inference to declare and initialize both arrays as follows:

var array1 = ["a", "b", "c", "d", "e"]
var array2 = ["a", "c", "d"]

@Antonio's solution is more performant, but this preserves ordering, if that's important:

var array1 = ["a", "b", "c", "d", "e"]
let array2 = ["a", "c", "d"]
array1 = array1.filter { !array2.contains($0) }

Tags:

Arrays

Ios

Swift