To find the item with the minimum number of occurrences in a Vector in Rust, you can use a HashMap to count the occurrences of each item and then find the item with the minimum count.
In this example,
std::collections::HashMap
.vec
containing several string elements.counts
to store the occurrences of each string in the vector. The keys of the HashMap will be the strings, and the values will be their counts.for
loop to iterate through each element in the vector. For each element, we increment its count in the HashMap using the entry
method to handle missing keys.min_item
and min_count
, to keep track of the item with the minimum occurrences and its count. min_count
is initialized to usize::MAX
to ensure any count will be lower initially.min_count
, we update min_item
and min_count
.println!
.use std::collections::HashMap;
fn main() {
// Step 2: Create a vector of strings
let vec = vec!["apple", "banana", "apple", "orange", "banana", "apple", "kiwi"];
// Step 3: Create a HashMap to count occurrences
let mut counts = HashMap::new();
// Step 4: Count occurrences of each item in the vector
for item in vec {
*counts.entry(item).or_insert(0) += 1;
}
// Step 5: Initialize variables to find the item with the minimum occurrences
let mut min_item = "";
let mut min_count = usize::MAX;
// Step 6: Iterate through the HashMap to find the item with the lowest count
for (item, &count) in &counts {
if count < min_count {
min_item = item;
min_count = count;
}
}
// Step 7: Print the item with the minimum occurrences
println!("Item with minimum occurrences: {}", min_item);
}
Item with minimum occurrences: kiwi
In this example,
std::collections::HashMap
.vec
containing several integer elements.counts
to store the occurrences of each integer in the vector. The keys of the HashMap will be the integers, and the values will be their counts.for
loop to iterate through each element in the vector. For each element, we increment its count in the HashMap using the entry
method to handle missing keys.min_item
and min_count
, to keep track of the item with the minimum occurrences and its count. min_count
is initialized to usize::MAX
to ensure any count will be lower initially.min_count
, we update min_item
and min_count
.println!
.use std::collections::HashMap;
fn main() {
// Step 2: Create a vector of integers
let vec = vec![1, 2, 3, 1, 2, 1, 4, 1, 5, 1];
// Step 3: Create a HashMap to count occurrences
let mut counts = HashMap::new();
// Step 4: Count occurrences of each item in the vector
for item in vec {
*counts.entry(item).or_insert(0) += 1;
}
// Step 5: Initialize variables to find the item with the minimum occurrences
let mut min_item = 0;
let mut min_count = usize::MAX;
// Step 6: Iterate through the HashMap to find the item with the lowest count
for (&item, &count) in &counts {
if count < min_count {
min_item = item;
min_count = count;
}
}
// Step 7: Print the item with the minimum occurrences
println!("Item with minimum occurrences: {}", min_item);
}
Item with minimum occurrences: 3
In this tutorial, we learned How to find the Item with Minimum Number of Occurrences in a Vector in Rust language with well detailed examples.