Program to find the most occurring element in an array of integers in C++

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

Example:

Input: {12, 6, 4, 12, 9, 1}

Output: 12

# Algorithm

  1. Taking array as input from the user.
  2. The outer loop picks all elements one by one.
  3. The inner loop finds the repetition of the picked element and compares it with the maximum so far.

Code:

#include<bits/stdc++.h>
using namespace std;

void repeated_number(int arr[], int n){

int i, j, max_count = 0; cout << "\nMost occurred number: ";

for (i = 0; i < n; i++){ int count = 1; for (j = i + 1; j < n; j++) if (arr[i] == arr[j]) count++;

if (count &gt; max_count)
  max_count = count;

}

// this loop checks if there are more than one elements that are repeated

for (i = 0; i < n; i++){ int count = 1; for (j = i + 1; j < n; j++) if (arr[i] == arr[j]) count++;

if (count == max_count)
  cout &lt;&lt; arr[i] &lt;&lt; endl;

} }

int main(){ int arr[100], n, i; cout << "Enter number of elements: "; cin >> n; cout << "\nEnter array: "; for (i = 0; i < n; i++) cin >> arr[i];

cout << "Original array: ";

for (i = 0; i < n; i++) cout << arr[i] << " ";

repeated_number(arr, n); return 0; }

Time complexity: O(n2)