0

I am creating a Quiz and I have an array of bools and I am trying to find a way to count how many "true" & "false" values there are, so I can present the results of the quiz to the user. I would like to store the results in a variable I can call at a later point.

I have looked at the set collection type but can`t seem to wrap my head around it. Do I initalise set to a bool?

answersArray = [true, false, true, false, true, false, true]

trueAnswersCount = set<Bool>()

6 Answers 6

3

You can't just use the native Set type since Sets don't hold duplicate values. For example, the following will emit {false, true}.

let answersArray = [true, false, true, false, true, false, true]
let trueAnswersCount = Set<Bool>(answersArray)

If you're willing to use classes from Cocoa Touch, NSCountedSet simplifies this quite a bit.

let answersArray = [true, false, true, false, true, false, true]

let countedSet = NSCountedSet()
countedSet.addObjectsFromArray(answersArray)

countedSet.countForObject(false) // 3
countedSet.countForObject(true) // 4

NSCountedSet is still a Set and only stores unique values, but it also keeps track of how many times each unique element was added to it.

Sign up to request clarification or add additional context in comments.

Comments

2

Why not just use filter()?

answersArray = [true, false, true, false, true, false, true]

let trueCount  = answersArray.filter { $0 == true }.count
let falseCount = answersArray.filter { $0 == false }.count

Comments

1

You can simply loop through your array and increment by one each time the value is true.

Here is a quick example :

var answersArray : [Bool]

answersArray = [true, false, true, false, true, false, true]

var nbTrueAnswers : Int = 0

for value in answersArray {
    if (value == true) {
        nbTrueAnswers++;
    }
}

println("nbTrueAnswers : \(nbTrueAnswers)")

Or see it on : http://swiftstub.com/184888077/

Let me know if it helped :)

2 Comments

Shorter: let nbTrueAnswers = reduce(answersArray, 0) { $0 + Int($1) } :)
@MartinR its not a shorter...its a shortest :P
0

You can easily use the functional programming power of Swift to obtain a very concise solution for your problem:

let trueAnswersCount = answersArray.filter{$0 == true}.count

Comments

0

You could make a function that takes a SequenceType and a value and returns the number of times the supplied value matches an element in the sequence:

func countMatches<Seq: SequenceType where Seq.Generator.Element: Equatable> 
                 (arr: Seq, v: Seq.Generator.Element) -> Int {

    return arr.reduce(0) {
        if $1 == v { return $0 + 1 }
        return $0
    }
}

let answersArray = [true, false, true, false, true, false, true]
countMatches(answersArray, true)  // 4
countMatches(answersArray, false) // 3

Or, for Swift 2:

extension SequenceType where Generator.Element: Equatable {
    func countMatches(v: Generator.Element) -> Int {
        return reduce(0) { 
            if $1 == v { return $0 + 1 }
            return $0
        }
    }
}

let answersArray = [true, false, true, false, true, false, true]
answersArray.countMatches(true)  // 4
answersArray.countMatches(false) // 3

3 Comments

Swift 1.2 does not need the empty parameter name _ .
Unless I am mistaken, that changed between Xcode 6.3 (Swift 1.2) and Xcode 7 (Swift 2).
My bad, you're correct; I was using Swift 2 when writing that first function.
0
    var wordCounts = [String: Int]()
    var allWords = ["this","is","a","test","this","is", "another", "test"]
    for word in allWords {
       if wordCounts[word] == nil {
          wordCounts[word] = 1
       } else {
          wordCounts[word]! += 1
       }
     }

    print(wordCounts) // prints: ["a": 1, "this": 2, "test": 2, "another": 1, "is": 2]
    print(wordCounts["this"]!) // get the count of word "this": 2
    allWords = Array(wordCounts.keys) // remove duplicates

or even faster:

    var wordCounts: NSCountedSet!
    var allWords = [true, true, false, false, false, false, true, false] as [Any]

    wordCounts = NSCountedSet(array: allWords)
    print(wordCounts.count(for: true)) // print: 3

see details here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.