A fast, compact and elegant way to do it is by using the reduce method:
let count = OSes.reduce(0) { $1 == "Android" ? $0 + 1 : $0 }
It's more compact than a for loop, and faster than a filter, because it doesn't generate a new array.
The reduce method takes an initial value, 0 in our case, and a closure, applied to each element of the array.
The closure takes 2 parameters:
- the value at the previous iteration (or the initial value, 0 in our case)
- the array element for the current iteration
The value returned by the closure is used as the first parameter in the next iteration, or as the return value of the reduce method when the last element has been processed
The closure simply checks if the current element is Android:
- if not, it returns the aggregate value (the first parameter passed to the closure)
- if yes, it returns that number plus one