I have two arrays
let toBeFiltered = ["star0", "star2", "star1", "star0", "star3", "star4"]
let theFilter = ["star1", "star3"]
How to filter the first array using the second array? Actually the theFilter
can be changed dynamically, e.g,
let theFilter = ["star2"]
or maybe
let theFilter = ["star0", "star4", "star2"]
Thanks for your help :)
While using Sets as proposed by Arsen is definitly most elegant, sometimes you want to keep duplicates and order:
this seems to be a theme today :) building on another great answer, I would suggest using the
intersect(_:)
method on aSet
:Use Set Operations
Read more: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html
You can also filter Struct array as well
For Set you must conforms the Hashable protocol