colindunn
colindunn

Reputation: 3163

Swift: Iterate through array and count Ints equal to 5

In Swift how would I iterate through the NSMutableArray of ints var numbers = [4,5,5,4,3] and count how many are equal to 5?

Upvotes: 0

Views: 144

Answers (2)

Sebastian
Sebastian

Reputation: 8164

You can use reduce for this:

let array = [4,5,5,4,3]
let fives = array.reduce(0, combine: { $0 + Int($1 == 5) })

Upvotes: 4

Dániel Nagy
Dániel Nagy

Reputation: 12015

One possible solution:

numbers.filter {$0 == 5}.count

Upvotes: 1

Related Questions