1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/// 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 of `value`.
/// * `value` - The value to count within the collection.
///
/// # Type Parameters
///
/// * `T` - The type of elements in the collection. Must implement `PartialEq`.
///
/// # Returns
///
/// * `usize` - The number of times `value` appears in `collection`.
///
/// # Examples
///
/// ```rust
/// use lowdash::count;
///
/// let numbers = vec![1, 2, 2, 3, 4, 2];
/// let result = count(&numbers, 2);
/// assert_eq!(result, 3);
/// ```
///
/// ```rust
/// 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);
/// ```
pub fn count<T>(collection: &[T], value: T) -> usize
where
T: PartialEq,
{
let mut count = 0;
for item in collection {
if *item == value {
count += 1;
}
}
count
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Debug, PartialEq)]
struct Person {
name: String,
age: u32,
}
#[test]
fn test_count_integers() {
let numbers = vec![1, 2, 2, 3, 4, 2];
let result = count(&numbers, 2);
assert_eq!(result, 3);
}
#[test]
fn test_count_strings() {
let strings = vec!["apple", "banana", "apple", "cherry", "banana"];
let result = count(&strings, "apple");
assert_eq!(result, 2);
}
#[test]
fn test_count_structs() {
let people = vec![
Person {
name: "Alice".to_string(),
age: 25,
},
Person {
name: "Bob".to_string(),
age: 30,
},
Person {
name: "Alice".to_string(),
age: 25,
},
Person {
name: "Carol".to_string(),
age: 35,
},
];
let count_alice = count(
&people,
Person {
name: "Alice".to_string(),
age: 25,
},
);
assert_eq!(count_alice, 2);
let count_bob = count(
&people,
Person {
name: "Bob".to_string(),
age: 30,
},
);
assert_eq!(count_bob, 1);
let count_dave = count(
&people,
Person {
name: "Dave".to_string(),
age: 40,
},
);
assert_eq!(count_dave, 0);
}
#[test]
fn test_count_with_empty_collection() {
let empty: Vec<i32> = vec![];
let result = count(&empty, 1);
assert_eq!(result, 0);
}
#[test]
fn test_count_with_no_matches() {
let numbers = vec![1, 2, 3, 4, 5];
let result = count(&numbers, 6);
assert_eq!(result, 0);
}
#[test]
fn test_count_with_all_matches() {
let numbers = vec![2, 2, 2, 2];
let result = count(&numbers, 2);
assert_eq!(result, 4);
}
#[test]
fn test_count_with_optionals() {
let collection = vec![Some(1), None, Some(2), Some(1), None, Some(3), Some(2)];
let result_some_1 = count(&collection, Some(1));
assert_eq!(result_some_1, 2);
let result_none = count(&collection, None);
assert_eq!(result_none, 2);
}
#[test]
fn test_count_with_floats() {
let float_collection = vec![1.1, 2.2, 2.2, 3.3, 4.4, 2.2];
let result = count(&float_collection, 2.2);
assert_eq!(result, 3);
}
#[test]
fn test_count_with_negatives() {
let numbers = vec![-1, -2, -2, -3, -4, -2];
let result = count(&numbers, -2);
assert_eq!(result, 3);
}
#[test]
fn test_count_preserves_order() {
let numbers = vec![3, 1, 2, 3, 2, 4, 1, 5];
let result = count(&numbers, 2);
assert_eq!(result, 2);
}
#[test]
fn test_count_with_custom_types() {
#[derive(Debug, PartialEq)]
struct Item {
id: u32,
value: String,
}
let items = vec![
Item {
id: 1,
value: "one".to_string(),
},
Item {
id: 2,
value: "two".to_string(),
},
Item {
id: 1,
value: "one".to_string(),
},
Item {
id: 3,
value: "three".to_string(),
},
];
let count_item1 = count(
&items,
Item {
id: 1,
value: "one".to_string(),
},
);
assert_eq!(count_item1, 2);
let count_item2 = count(
&items,
Item {
id: 2,
value: "two".to_string(),
},
);
assert_eq!(count_item2, 1);
let count_item4 = count(
&items,
Item {
id: 4,
value: "four".to_string(),
},
);
assert_eq!(count_item4, 0);
}
#[test]
fn test_count_with_characters() {
let chars = vec!['a', 'b', 'a', 'c', 'b', 'd'];
let result = count(&chars, 'a');
assert_eq!(result, 2);
}
#[test]
fn test_count_with_nan_floats() {
let float_collection = vec![std::f64::NAN, 2.2, std::f64::NAN, 4.4];
let result_nan = count(&float_collection, std::f64::NAN);
// Note: In Rust, NaN != NaN, so each comparison with NaN returns false
assert_eq!(result_nan, 0);
let result_2_2 = count(&float_collection, 2.2);
assert_eq!(result_2_2, 1);
}
#[test]
fn test_count_with_string_slices() {
let words = vec!["hello", "world", "hello", "rust"];
let result = count(&words, "hello");
assert_eq!(result, 2);
}
}