Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Filter Array Using Element in Another Array in Swift?

Tags:

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 :)

like image 922
Asep Bagja Priandana Avatar asked Oct 03 '15 17:10

Asep Bagja Priandana


1 Answers

Use Set Operations

Set(toBeFiltered).intersection(Set(theFilter)) 

Read more: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html

like image 157
Arsen Avatar answered Sep 30 '22 08:09

Arsen