Skip to content
Related Articles

Related Articles

Counting number of elements in an array in Julia – count() Method
  • Last Updated : 26 Mar, 2020

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

# 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]))

chevron_right


Output:

3
2
4
3

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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]))

chevron_right


Output:

0
2
3
My Personal Notes arrow_drop_up
Recommended Articles
Page :