Home »
C++ Programs
C++ program to count total occurrences of each element in an array using class
Submitted by Shubh Pachori, on October 09, 2022
Problem statement
Given an array, we to count the total occurrences of each element in an array using the class and object approach.
Example:
Input:
Enter Array Elements :
[0]: 1
[1]: 2
[2]: 3
[3]: 4
[4]: 5
[5]: 5
[6]: 4
[7]: 3
[8]: 2
[9]: 1
Output:
Total Occurrences of each Element in Array :
1 occurred 2 time
2 occurred 2 time
3 occurred 2 time
4 occurred 2 time
5 occurred 2 time
C++ code to count total occurrences of each element in an array using the class and object approach
#include <bits/stdc++.h>
using namespace std;
// create a class
class Array {
// private data members
private:
int arr[10];
// public member functions
public:
// getArray() function to insert elements in array
void getArray() {
cout << "Enter Array Elements :" << endl;
for (int index = 0; index < 10; index++) {
cout << "[" << index << "]: ";
cin >> arr[index];
}
}
// occuranceArray() function to count total
// Occurrences of each Element in array
void occuranceArray() {
// initializing int type variables to
// perform operations
int index_1, index_2, count;
// vector to check all Elements of the
// if they are repeated
vector<bool> visited(10, false);
cout << "\nTotal Occurrences of each Element in Array :" << endl;
// for loop to traverse the whole array
for (index_1 = 0; index_1 < 10; index_1++) {
// if condition to check if the number is
// visited recently or not
if (visited[index_1] == true) continue;
count = 1;
// for loop to count total Occurrences of
// each Element of array
for (index_2 = index_1 + 1; index_2 < 10; index_2++) {
// if condition to check if the number in array at
// index_1 and at index_2 are equal or not
if (arr[index_1] == arr[index_2]) {
visited[index_2] = true;
count++;
}
}
cout << arr[index_1] << " occurred " << count << " time" << endl;
}
}
};
int main() {
// create an object
Array A;
// calling getArray() function to
// insert Elements in array
A.getArray();
// calling occuranceArray() function to count total
// Occurrences of each Element in array
A.occuranceArray();
return 0;
}
Output
Enter Array Elements :
[0]: 8
[1]: 4
[2]: 3
[3]: 5
[4]: 8
[5]: 0
[6]: 9
[7]: 6
[8]: 1
[9]: 7
Total Occurrences of each Elements in Array :
8 occurred 2 time
4 occurred 1 time
3 occurred 1 time
5 occurred 1 time
0 occurred 1 time
9 occurred 1 time
6 occurred 1 time
1 occurred 1 time
7 occurred 1 time
Explanation
In the above code, we have created a class Array, one int type array data members arr[10] to store the elements of the array, and public member functions getArray() and occuranceArray() to store the array elements and to count the total occurrence of each element in an array.
In the main() function, we create an object A of class Array, reading the inputted array by the user using getArray() function, and finally call the occuranceArray() member function to count total occurrences of each element in an array. The occuranceArray() function contains the logic to count the total occurrences of each element in an array and printing the result.
C++ Class and Object Programs (Set 2) »