C++ Exercises: Find the most occurring element in an array of integers
C++ Array: Exercise-7 with Solution
Write a C++ program to find the most occurring element in an array of integers.
Pictorial Presentation:
Sample Solution:
C++ Code :
#include<iostream>
using namespace std;
void most_occurred_number(int nums[], int size)
{
int max_count = 0;
cout << "\nMost occurred number: ";
for (int i=0; i<size; i++)
{
int count=1;
for (int j=i+1;j<size;j++)
if (nums[i]==nums[j])
count++;
if (count>max_count)
max_count = count;
}
for (int i=0;i<size;i++)
{
int count=1;
for (int j=i+1;j<size;j++)
if (nums[i]==nums[j])
count++;
if (count==max_count)
cout << nums[i] << endl;
}
}
int main()
{
int nums[] = {4, 5, 9, 12, 9, 22, 45, 7};
int n = sizeof(nums)/sizeof(nums[0]);
cout << "Original array: ";
for (int i=0; i < n; i++)
cout << nums[i] <<" ";
most_occurred_number(nums, n);
}
Sample Output:
Original array: 4 5 9 12 9 22 45 7 Most occurred number: 9
Flowchart:
C++ Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a C++ program to find all elements in array of integers which have at-least two greater elements.
Next: Write a C++ program to find the next greater element of every element of a given array of integers. Ignore those elements which have no greater element.
What is the difficulty level of this exercise?