Swift - How do I count the items in an array of a specific value?

forrest

I have an array with 200 items. These are either true or false:

let shouldMascotChangeVotes: [Bool] = [false, false, false, true, false, true, true, true, false, true, true, true, true, false, true, true, false, true, true, true, false, true, true, true, true, true, true, true, false, true, false, true, false, true, true, false, false, true, true, false, false, true, true, true, false, true, false, true, true, false, true, true, false, true, false, false, true, false, true, true, false, false, true, false, true, true, true, false, true, true, false, false, true, false, true, true, false, false, false, true, false, true, true, false, true, true, true, true, true, true, true, false, true, false, true, false, true, true, true, true, true, true, true, false, true, true, false, true, true, true, true, true, true, true, false, true, true, false, true, true, false, true, true, true, true, true, false, false, false, false, true, true, true, false, true, true, false, false, true, false, false, true, true, true, true, false, true, true, true, true, false, true, true, false, true, false, false, true, true, false, true, false, false, false, true, false, false, false, true, false, true, true, false, true, true, false, true, true, true, false, false, false, true, false, true, false, true, true, true, true, false, true, false, false, true, true, true, true, true, false]

I know how to count the total items in the array but I need to be able to count just the true or false values in the array. How do I do that?

shouldMascotChangeVotes.count // returns 200
holex

I'm not sure how important the actual performance to you, but you can do such thing, for instance.

import UIKit

let shouldMascotChangeVotes: [Bool] = [false, false, false, true, false, true, true, true, false, true, true, true, true, false, true, true, false, true, true, true, false, true, true, true, true, true, true, true, false, true, false, true, false, true, true, false, false, true, true, false, false, true, true, true, false, true, false, true, true, false, true, true, false, true, false, false, true, false, true, true, false, false, true, false, true, true, true, false, true, true, false, false, true, false, true, true, false, false, false, true, false, true, true, false, true, true, true, true, true, true, true, false, true, false, true, false, true, true, true, true, true, true, true, false, true, true, false, true, true, true, true, true, true, true, false, true, true, false, true, true, false, true, true, true, true, true, false, false, false, false, true, true, true, false, true, true, false, false, true, false, false, true, true, true, true, false, true, true, true, true, false, true, true, false, true, false, false, true, true, false, true, false, false, false, true, false, false, false, true, false, true, true, false, true, true, false, true, true, true, false, false, false, true, false, true, false, true, true, true, true, false, true, false, false, true, true, true, true, true, false]

let countableSet = NSCountedSet()

shouldMascotChangeVotes.forEach { (item: Bool) in
    countableSet.add(item)
}

debugPrint(countableSet.count(for: true)) // 125
debugPrint(countableSet.count(for: false)) // 75

NOTE: maybe not the most effective solution but it does the job; that snippet is coming from Playground directly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to count specific items in array in swift

From Dev

How to count specific items in array

From Dev

Count number of items in an array with a specific property value

From Dev

How do I count the number of items in an array that start with the highest number?

From Dev

How do I count the number of items in an array that start with the highest number?

From Dev

How do I remove/filter specific items from an array?

From Dev

How do I count the number of occurences of a specific value in a group?

From Dev

How do I nest an array of items dividing them by their first value?

From Dev

How do I nest an array of items dividing them by their first value?

From Dev

Swift: How do i insert item to array with specific order?

From Dev

How do I put a specific integer into the middle of a sorted array in Swift?

From Dev

How do I update specific key value of the local storage array?

From Dev

How do I get value of specific array element by secondary key?

From Dev

How do I return a specific value if a string is not contained in an array

From Dev

How do I get a value from an associative array using Swift

From Dev

How to separate sentence into array items, by a specific count of words

From Dev

How do I select specific index items from a multi-dimensional array in jQuery

From Dev

Count items in array by property value

From Java

How do I get the count of a Swift enum?

From Dev

How do I create an array of tuples in Swift where one of the items in the tuple is optional?

From Dev

Count of Items in an Array within an Array on Swift 3

From Dev

How do I group by the count value in sql

From Dev

Ruby - How do I sort an array based on the value of nested hash items?

From Dev

numpy - how do I count the occurrence of items in nested lists by index?

From Dev

numpy - how do I count the occurrence of items in nested lists by index?

From Dev

How do i call specific value by index of array in public static that return an array

From Dev

How do I get a count of values in an array, in a range or above a threshold value?

From Dev

How do I get a count of values in an array, in a range or above a threshold value?

From Dev

How do I know if an ArrayList contains a specific items in mongoDB?

Related Related

  1. 1

    how to count specific items in array in swift

  2. 2

    How to count specific items in array

  3. 3

    Count number of items in an array with a specific property value

  4. 4

    How do I count the number of items in an array that start with the highest number?

  5. 5

    How do I count the number of items in an array that start with the highest number?

  6. 6

    How do I remove/filter specific items from an array?

  7. 7

    How do I count the number of occurences of a specific value in a group?

  8. 8

    How do I nest an array of items dividing them by their first value?

  9. 9

    How do I nest an array of items dividing them by their first value?

  10. 10

    Swift: How do i insert item to array with specific order?

  11. 11

    How do I put a specific integer into the middle of a sorted array in Swift?

  12. 12

    How do I update specific key value of the local storage array?

  13. 13

    How do I get value of specific array element by secondary key?

  14. 14

    How do I return a specific value if a string is not contained in an array

  15. 15

    How do I get a value from an associative array using Swift

  16. 16

    How to separate sentence into array items, by a specific count of words

  17. 17

    How do I select specific index items from a multi-dimensional array in jQuery

  18. 18

    Count items in array by property value

  19. 19

    How do I get the count of a Swift enum?

  20. 20

    How do I create an array of tuples in Swift where one of the items in the tuple is optional?

  21. 21

    Count of Items in an Array within an Array on Swift 3

  22. 22

    How do I group by the count value in sql

  23. 23

    Ruby - How do I sort an array based on the value of nested hash items?

  24. 24

    numpy - how do I count the occurrence of items in nested lists by index?

  25. 25

    numpy - how do I count the occurrence of items in nested lists by index?

  26. 26

    How do i call specific value by index of array in public static that return an array

  27. 27

    How do I get a count of values in an array, in a range or above a threshold value?

  28. 28

    How do I get a count of values in an array, in a range or above a threshold value?

  29. 29

    How do I know if an ArrayList contains a specific items in mongoDB?

HotTag

Archive