c++ array count freqeancy

To count the frequency of elements in an array in C++, you can follow these steps:

  1. Declare an integer array to store the elements.
  2. Initialize the array with values.
  3. Declare a new integer array to store the frequency of each element.
  4. Initialize all the elements of the frequency array to zero.
  5. Iterate through each element in the input array.
  6. For each element, iterate through the input array again to count its frequency.
  7. Update the corresponding element in the frequency array.
  8. Print the frequency array to display the frequency of each element.

Here is an example code snippet that demonstrates these steps:

#include <iostream>
using namespace std;

int main() {
    int arr[] = {1, 2, 3, 2, 4, 2, 3, 1, 3, 4};
    int n = sizeof(arr) / sizeof(arr[0]);

    int freq[100] = {0};

    for (int i = 0; i < n; i++) {
        freq[arr[i]]++;
    }

    for (int i = 0; i < 100; i++) {
        if (freq[i] != 0) {
            cout << "Frequency of " << i << " is " << freq[i] << endl;
        }
    }

    return 0;
}

This code will output the frequency of each element in the array:

Frequency of 1 is 2
Frequency of 2 is 3
Frequency of 3 is 3
Frequency of 4 is 2

Please note that the size of the frequency array should be large enough to accommodate the range of elements in the input array. In this example, we assumed the elements range from 0 to 99. You can adjust the size of the frequency array accordingly based on your specific needs.