Counting number of elements in an array in Julia – count() Method

The count() is an inbuilt function in julia which is used to count the number of elements in the specified array for which the given predicate p returns true and if p is omitted, counts the number of true elements in the given collection of boolean values.

Syntax:
count(p, itr)
or
count(itr)

Parameters:

  • p: Specified set of instructions.
  • itr: Specified collection of boolean values.

Returns: It returns the count of the number of elements in the specified array for which the given predicate p returns true and if p is omitted, counts the number of true elements in the given collection of boolean values.

Example 1:




# Julia program to illustrate 
# the use of count() method
  
# Getting the count of the number
# of elements in the specified array
# for which the given predicate p 
# returns true.
println(count(i->(i<= 3), [1, 2, 3, 4, 5]))
println(count(i->(i>3), [1, 2, 3, 4, 5]))
println(count(i->(2<= i<= 5), [1, 2, 3, 4, 5]))
println(count(i->(i>= 0), [1, 2, 3]))


Output:

3
2
4
3

Example 2:




# Julia program to illustrate 
# the use of count() method
  
# Getting the counts of number of true elements
# in the given collection of boolean values.
println(count([false, false, false]))
println(count([true, false, true]))
println(count([true, true, true]))


Output:

0
2
3