count
count(p, itr) -> Integer
Count the number of elements in itr
for which predicate p
returns true
.
Examples
Count the number of elements satisfying a predicate:
julia> count(x -> x > 5, [1, 10, 2, 8, 7, 3])
3
This example counts the number of elements in the array that are greater than 5.
julia> count(iseven, [1, 2, 3, 4, 5, 6])
3
Here, the count
function is used to count the number of even elements in the array.
julia> count(isequal("apple"), ["apple", "banana", "orange", "apple"])
2
In this case, the count
function is used to count the occurrences of the string "apple" in the array.
Common mistake example:
julia> count(x -> x > 5, [])
0
When applying count
on an empty collection, the result will be 0. It is important to handle this case to avoid potential errors.
julia> count(x -> x > 5, [1, 2, 3, 4, 5])
0
If there are no elements satisfying the predicate, the count will also be 0. Ensure that the predicate matches the elements you are trying to count.
See Also
User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.