pub fn count<T>(collection: &[T], value: T) -> usizewhere
T: PartialEq,Expand description
Counts the number of occurrences of a specific value in a collection.
This function iterates over a slice of items and returns the number of times
the specified value appears in the collection.
Time Complexity: O(n), where n is the number of elements in the collection.
§Arguments
collection- A slice of items in which to count occurrences ofvalue.value- The value to count within the collection.
§Type Parameters
T- The type of elements in the collection. Must implementPartialEq.
§Returns
usize- The number of timesvalueappears incollection.
§Examples
use lowdash::count;
let numbers = vec![1, 2, 2, 3, 4, 2];
let result = count(&numbers, 2);
assert_eq!(result, 3);use lowdash::count;
#[derive(Debug, PartialEq)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Alice".to_string(), age: 25 },
];
let count_alice = count(&people, Person { name: "Alice".to_string(), age: 25 });
assert_eq!(count_alice, 2);