Program to find the most repeated element from the array

0 Suresh Chand March 29, 2021

In this example, We will find the most repeated number from the given array.

To understand this example, You must have knowledge in following topics

First we will take array as input and we will find all most ocuring element in array using c++ programming language.

Let’s look and code to make clear concepts

#include <iostream>
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 > 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 << arr[i] << endl;
    }
}

int main()
{
    int arr[100], n, i;
    cout << "Enter number of elements in array: ";
    cin >> n;
    cout << "\nEnter array elements: ";
    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;
}

The output of above program is

Enter number of elements in array: 7

Enter array elements: 1
6
4
6
2
3
6
Original array: 1 6 4 6 2 3 6 
Most occurred number: 6   
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Join Our Newsletter Now

Get the latest tutorials, examples and projects updates.

!

Ads

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Disable Ad Block

Want Same System