To find the item with the maximum 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 maximum count.
In this example,
#include <iostream>
, #include <vector>
, and #include <unordered_map>
for using input-output streams, vectors, and unordered maps respectively.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.max_item
and max_count
, to keep track of the item with the maximum occurrences and its count. max_item
is initialized as an empty string, and max_count
is initialized to 0.max_count
, we update max_item
and max_count
.std::cout
.#include <iostream>
#include <vector>
#include <unordered_map>
int main() {
// Step 2: Create a vector of strings
std::vector<std::string> vec = {"apple", "banana", "apple", "orange", "banana", "apple"};
// 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 maximum occurrences
std::string max_item;
int max_count = 0;
// Step 6: Iterate through the unordered_map to find the item with the highest count
for (const auto& pair : counts) {
if (pair.second > max_count) {
max_item = pair.first;
max_count = pair.second;
}
}
// Step 7: Print the item with the maximum occurrences
std::cout << "Item with maximum occurrences: " << max_item << std::endl;
return 0;
}
Item with maximum occurrences: apple
In this example,
#include <iostream>
, #include <vector>
, and #include <unordered_map>
for using input-output streams, vectors, and unordered maps respectively.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.max_item
and max_count
, to keep track of the item with the maximum occurrences and its count. max_item
is initialized as 0, and max_count
is initialized to 0.max_count
, we update max_item
and max_count
.std::cout
.#include <iostream>
#include <vector>
#include <unordered_map>
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 maximum occurrences
int max_item = 0;
int max_count = 0;
// Step 6: Iterate through the unordered_map to find the item with the highest count
for (const auto& pair : counts) {
if (pair.second > max_count) {
max_item = pair.first;
max_count = pair.second;
}
}
// Step 7: Print the item with the maximum occurrences
std::cout << "Item with maximum occurrences: " << max_item << std::endl;
return 0;
}
Item with maximum occurrences: 1
In this tutorial, we learned How to find the Item with Maximum Number of Occurrences in a Vector in C++ language with well detailed examples.