C++ Online Compiler
Example: FrequencyCount_AuxiliaryArray in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// FrequencyCount_AuxiliaryArray #include <iostream> #include <vector> // Using std::vector for the frequency array #include <algorithm> // For std::max_element int main() { // Step 1: Initialize the array std::vector<int> arr = {5, 2, 8, 5, 1, 2, 5}; int n = arr.size(); // Step 2: Determine the maximum element to size the frequency array // This approach assumes non-negative integers. int max_element = 0; if (n > 0) { max_element = *std::max_element(arr.begin(), arr.end()); } // Step 3: Create a frequency array (vector) initialized to zeros // Size should be max_element + 1 to accommodate elements from 0 to max_element std::vector<int> freq(max_element + 1, 0); // Step 4: Iterate through the input array and update frequencies for (int i = 0; i < n; i++) { freq[arr[i]]++; } std::cout << "Element frequencies using Auxiliary Array:" << std::endl; // Step 5: Iterate through the frequency array and print elements with count > 0 for (int i = 0; i <= max_element; i++) { if (freq[i] > 0) { std::cout << "Element " << i << ": " << freq[i] << " time(s)" << std::endl; } } return 0; }
Output
Clear
ADVERTISEMENTS