Count Frequencies of Elements in an Array Using Hashing in C++

 

 

Program Overview

This C++ program demonstrates how to count the frequencies of elements in an array using a hash map (unordered_map from the STL). The program takes an array of integers as input and outputs the frequency of each element.

Program Structure

  • Includes Necessary Libraries: The program includes the iostream and unordered_map libraries for input/output operations and using hash maps, respectively.
  • Main Function: This is where the program execution begins. It initializes the array and calls the function to count frequencies.
  • countFrequencies Function: This function accepts an array and its size, and uses an unordered map to count and store the frequency of each element.
  • Output: The program outputs each unique element and its corresponding frequency.

Code Implementation


#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

/**
 * Function to count the frequencies of elements in an array.
 * 
 * @param arr: Vector of integers containing the elements.
 */
void countFrequencies(const vector<int> &arr) {
    unordered_map<int, int> frequencyMap;

    // Count frequency of each element
    for (int num : arr) {
        frequencyMap[num]++;
    }

    // Output the frequencies
    cout << "Element Frequencies:" << endl;
    for (const auto &pair : frequencyMap) {
        cout << "Element: " << pair.first << ", Frequency: " << pair.second << endl;
    }
}

/**
 * Main function to execute the program.
 */
int main() {
    // Initialize an array of integers
    vector<int> arr = {1, 2, 2, 3, 1, 4, 4, 4, 5};

    // Count frequencies
    countFrequencies(arr);

    return 0;
}
    

Explanation of the Code

  1. Libraries Included:
    • <iostream>: For standard input and output.
    • <unordered_map>: To use the hash map for counting frequencies.
    • <vector>: To use vectors as dynamic arrays.
  2. countFrequencies Function:
    • Takes a vector of integers as an argument.
    • Uses an unordered_map to count the frequency of each element.
    • Iterates through the array and increments the count in the hash map.
    • Finally, it prints each unique element with its corresponding frequency.
  3. main Function:
    • Initializes a vector of integers with some sample values.
    • Calls the countFrequencies function to process the array.
  4. Compilation and Execution Instructions:
    • Instructions on how to save, compile, and run the program are provided.

How to Compile and Run

To compile and run this program, follow these steps:

    1. Save the code in a file named count_frequencies.cpp.
    2. Open your terminal or command prompt.
    3. Navigate to the directory where the file is saved.
    4. Compile the program using the following command:
g++ count_frequencies.cpp -o count_frequencies
    1. Run the compiled program:
./count_frequencies

Conclusion

This program effectively uses hashing to count the frequencies of elements in an array. The unordered_map provides an efficient way to map elements to their frequencies, allowing for quick access and updates. This approach is preferable in scenarios where performance is critical.

 

Leave a Reply

Your email address will not be published. Required fields are marked *