C++ Online Compiler
Example: FrequencyCount_HashMap in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// FrequencyCount_HashMap #include <iostream> #include <vector> #include <map> // For std::map int main() { // Step 1: Initialize the array std::vector<int> arr = {5, 2, 8, 5, 1, 2, 5}; // Step 2: Create a std::map to store element frequencies // Keys will be array elements, values will be their counts std::map<int, int> frequencies; // Step 3: Iterate through the array and populate the map for (int element : arr) { frequencies[element]++; // Increments count for existing key, or inserts new key with count 1 } std::cout << "Element frequencies using Hash Map (std::map):" << std::endl; // Step 4: Iterate through the map and print key-value pairs // std::map iterates in sorted key order for (const auto& pair : frequencies) { std::cout << "Element " << pair.first << ": " << pair.second << " time(s)" << std::endl; } return 0; }
Output
Clear
ADVERTISEMENTS