pastebin

Paste Search Dynamic
Recent pastes
countFreq
  1. // CPP program to count frequencies of array items
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. void countFreq(int arr[], int n)
  6. {
  7.         // Mark all array elements as not visited
  8.         int  visited[10000];
  9.  
  10.         // Traverse through array elements and
  11.         // count frequencies
  12.         for (int i = 0; i < n; i++) {
  13.  
  14.                 // Skip this element if already processed
  15.                 if (visited[i] == 0)
  16.                         continue;
  17.  
  18.                 // Count frequency
  19.                 int count = 1;
  20.                 for (int j = i + 1; j < n; j++) {
  21.                         if (arr[i] == arr[j]) {
  22.                                 visited[j] = 0;
  23.                                 count++;
  24.                         }
  25.                 }
  26.                 cout << arr[i] << " " << count << endl;
  27.         }
  28. }
  29.  
  30. int main()
  31. {
  32.         int arr[] = { 10, 20, 20, 10, 10, 20, 5, 20 };
  33.         int n = sizeof(arr) / sizeof(arr[0]);
  34.         countFreq(arr, n);
  35.         return 0;
  36. }
  37.  
Parsed in 0.011 seconds