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:



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 Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.