So I have an array of items, we’ll make it Strings for the example but they’re actually custom objects:
let original = ["apple1", "apple2", "banana1", "banana2", "carrot1", "carrot2", "donut1", "donut2"]
and then I filter the array to only get the “1” foods:
let filtered = original.filter { $0.contains("1") }
Then I want to get a random element from my filtered array and remove it when I do:
let randomIndex = filtered.getRandomIndex // returns a random index in the array
let randomElement = filtered[randomIndex]
filtered.remove(at: randomIndex)
Then when the filtered array contains no more data, I want to reset it back to its original state.
Option 1 is to create three arrays and reset the filtered
array when it’s empty:
// original - all strings
// filtered - filtered strings
// ogFiltered - original set of filtered strings
if filtered.count == 0 {
filtered = ogFiltered
}
Option 2 is to refilter the original array when filtered
is empty:
if filtered.count == 0 {
filtered = original.filter { $0.contains("1") }
}
So my question is, which is the better way to do this? Is it better/worse to have more arrays or more computations? Thanks!