filter the not selected array in swift in swift

To filter an array in swift, you can use the filter method on the array.

main.swift
let numbers = [1, 2, 3, 4, 5]
let selected = [2, 4, 5]

let notSelected = numbers.filter { !selected.contains($0) }

print(notSelected) // [1, 3]
146 chars
7 lines

The closure passed to the filter method will return true for all elements that should be included in the resulting array. In this example, we are using the contains method on the selected array to check if the current number should be included in the notSelected array. The ! operator negates the result, so the filter method will include all numbers that are not selected.

gistlibby LogSnag