To find the item with the minimum number of occurrences in a Vector in C++, you can use an unordered_map to count the occurrences of each item and then find the item with the minimum count.
In this example,
<iostream>
, <vector>
, and <unordered_map>
.vec
containing several string elements.counts
to store the occurrences of each string in the vector. The keys of the unordered_map 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 unordered_map.min_item
and min_count
, to keep track of the item with the minimum occurrences and its count. min_count
is initialized to INT_MAX
to ensure any count will be lower initially.min_count
, we update min_item
and min_count
.std::cout
.#include <iostream>
#include <vector>
#include <unordered_map>
#include <climits>
int main() {
// Step 2: Create a vector of strings
std::vector<std::string> vec = {"apple", "banana", "apple", "orange", "banana", "apple", "kiwi"};
// Step 3: Create an unordered_map to count occurrences
std::unordered_map<std::string, int> counts;
// Step 4: Count occurrences of each item in the vector
for (const auto& item : vec) {
counts[item]++;
}
// Step 5: Initialize variables to find the item with the minimum occurrences
std::string min_item;
int min_count = INT_MAX;
// Step 6: Iterate through the unordered_map to find the item with the lowest count
for (const auto& pair : counts) {
if (pair.second < min_count) {
min_item = pair.first;
min_count = pair.second;
}
}
// Step 7: Print the item with the minimum occurrences
std::cout << "Item with minimum occurrences: " << min_item << std::endl;
return 0;
}
Item with minimum occurrences: kiwi
In this example,
<iostream>
, <vector>
, and <unordered_map>
.vec
containing several integer elements.counts
to store the occurrences of each integer in the vector. The keys of the unordered_map 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 unordered_map.min_item
and min_count
, to keep track of the item with the minimum occurrences and its count. min_count
is initialized to INT_MAX
to ensure any count will be lower initially.min_count
, we update min_item
and min_count
.std::cout
.#include <iostream>
#include <vector>
#include <unordered_map>
#include <climits>
int main() {
// Step 2: Create a vector of integers
std::vector<int> vec = {1, 2, 3, 1, 2, 1, 4, 1, 5, 1};
// Step 3: Create an unordered_map to count occurrences
std::unordered_map<int, int> counts;
// Step 4: Count occurrences of each item in the vector
for (const auto& item : vec) {
counts[item]++;
}
// Step 5: Initialize variables to find the item with the minimum occurrences
int min_item = 0;
int min_count = INT_MAX;
// Step 6: Iterate through the unordered_map to find the item with the lowest count
for (const auto& pair : counts) {
if (pair.second < min_count) {
min_item = pair.first;
min_count = pair.second;
}
}
// Step 7: Print the item with the minimum occurrences
std::cout << "Item with minimum occurrences: " << min_item << std::endl;
return 0;
}
Item with minimum occurrences: 3
In this tutorial, we learned How to find the Item with Minimum Number of Occurrences in a Vector in C++ language with well detailed examples.