fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void findFrequency(int arr[], int size) {
  6. bool counted[100] = {false};
  7.  
  8. for (int i = 0; i < size; i++) {
  9. if (!counted[arr[i]]) {
  10. int count = 0;
  11.  
  12. for (int j = 0; j < size; j++) {
  13. if (arr[j] == arr[i]) {
  14. count++;
  15. }
  16. }
  17.  
  18. counted[arr[i]] = true;
  19.  
  20. cout << "Element " << arr[i] << " occurs " << count << " time(s)." << endl;
  21. }
  22. }
  23. }
  24.  
  25. int main() {
  26. int arr[100], size;
  27. cout << "Enter the number of elements in the array: ";
  28. cin >> size;
  29.  
  30. cout << "Enter the elements of the array: ";
  31. for (int i = 0; i < size; i++) {
  32. cin >> arr[i];
  33. }
  34.  
  35. findFrequency(arr, size);
  36.  
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0.01s 5284KB
stdin
11 12 13
stdout
Enter the number of elements in the array: Enter the elements of the array: Element 12 occurs 1 time(s).
Element 13 occurs 1 time(s).
Element 0 occurs 9 time(s).