Solution
This is a problem where Function Programming is a great help.
Given these 2 arrays
let words = ["ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN"]
let shouldRemove = ["TRUE", "TRUE", "TRUE", "FALSE", "TRUE", "TRUE", "FALSE"]
You can perform filter this way
let filtered = zip(words, shouldRemove).filter { $0.1 == "TRUE" }.map { $0.0 }
// ["ONE", "TWO", "THREE", "FIVE", "SIX"]
How does it work?
zip(words, shouldRemove)
This create a sequence where the n-th element of the first array is paired with the n-th element of the second array. So each element of this sequence has 2 components (one from words and one from shouldRemove).
zip(words, shouldRemove)
---------| ---------
ONE | TRUE
---------| ---------
TWO | TRUE
---------| ---------
THREE | TRUE
---------| ---------
FOUR | FALSE
---------| ---------
FIVE | TRUE
---------| ---------
SIX | TRUE
---------| ---------
SEVEN | FALSE
---------| ---------
Next
.filter { $0.1 == "TRUE" }
This filter the sequence leaving only the elements where the second component is the string "TRUE"
---------| ---------
ONE | TRUE
---------| ---------
TWO | TRUE
---------| ---------
THREE | TRUE
---------| ---------
FIVE | TRUE
---------| ---------
SIX | TRUE
---------| ---------
Next
.map { $0.0 }
Finally for each element, only the first component is returned (the one from words).
---------
ONE
---------
TWO
---------
THREE
---------
FIVE
---------
SIX
---------
Why did I rename the names of your arrays?
- In Swift we don't use the type of a variable into it's name. The name of a variable should represent it's value.
- Names of your arrays are inverted. You named
toBeRemoved the array with the content. Shouldn't it be the name with the "TRUE"/"FALSE"?