×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object Programs

C++ program to find the most occurring element in an array of integers using class

Submitted by Shubh Pachori, on September 13, 2022

Problem statement

Given an array of integers, we have to find the most occurring element in an array of integers using the class and object approach.

Example:

Input: 
[0]: 9
[1]: 9
[2]: 8
[3]: 1
[4]: 5
[5]: 9
[6]: 2
[7]: 8
[8]: 2
[9]: 4

Output: 
Most occurred number: 9

C++ code to find the most occurring element in an array of integers using the class and object approach

#include <iostream>
using namespace std;

// create a class
class Array {
  // private data member
 private:
  int arr[10];

  // public member functions
 public:
  // getArray() function to insert 
  // array elements
  void getArray() {
    for (int index = 0; index < 10; index++) {
      cout << "[" << index << "]: ";
      cin >> arr[index];
    }
  }

  // countNumber() function to find
  // the most occurred number
  void countNumber() {
    // initialising int type variable to 
    // perform operations
    int max_count = 0, count = 0, index_1, index_2;

    cout << "\nMost occurred number: ";

    // for loop to traverse the whole loop
    for (int index_1 = 0; index_1 < 10; index_1++) {
      // initialise the value of count 1
      count = 1;

      // for loop to count occurrence of all elements of the array
      for (index_2 = index_1 + 1; index_2 < 10; index_2++) {
        // if condition to check for the number is occurring or not
        if (arr[index_1] == arr[index_2]) {
          // increment of 1 at every iteration
          count++;
        }
      }

      // if condition to check for the most occurred number
      if (count > max_count) {
        // most occurred number is copied in the max_count
        max_count = count;
      }
    }

    // for loop to traverse the whole Array
    for (index_1 = 0; index_1 < 10; index_1++) {
      // initialise the value of count 1
      count = 1;

      // for loop to count occurrence of all elements of the array
      for (index_2 = index_1 + 1; index_2 < 10; index_2++) {
        // if condition to check for the number is occurring or not
        if (arr[index_1] == arr[index_2]) {
          // increment of 1 at every iteration
          count++;
        }
      }

      // if condition to check for the most occurred number
      if (count == max_count) {
        // most occured number is printed
        cout << arr[index_1] << endl;
      }
    }
  }
};

int main() {
  // create an object
  Array A;

  // calling getArray() function to insert the array
  A.getArray();

  // calling countNumber() function to find
  // the most occurred number
  A.countNumber();

  return 0;
}

Output

[0]: 8
[1]: 4
[2]: 3
[3]: 5
[4]: 8
[5]: 0
[6]: 9
[7]: 6
[8]: 1
[9]: 7

Most occurred number: 8

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 countNumber() to store the array elements and to find the most occurred number in the array.

In the main() function, we are creating an object A of class Array, reading the inputted array by the user using getArray() function, and finally calling the countNumber() member function to find the most occurred number in the array. The countNumber() function contains the logic to find the most occurred number in the array and printing the result.

C++ Class and Object Programs (Set 2) »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.