4

I had tried the following code to get most occurring element in an array. It is working well but the only problem is when there are two or more elements having the same number of occurrence and equal to most occurring element, it just shows the first element scanned. Please help me through this.

#include <iostream>
using namespace std;
int main()
{
    int i,j,a[5];
    int popular = a[0];
    int temp=0, tempCount, count=1;
    cout << "Enter the elements: " << endl;
    for(i=0;i<5;i++)
        cin >> a[i];
    for (i=0;i<5;i++)
    {
        tempCount = 0;
        temp=a[i];
        tempCount++;
        for(j=i+1;j<5;j++)
        {
            if(a[j] == temp)
            {
                tempCount++;
                if(tempCount > count)
                {
                    popular = temp;
                    count = tempCount;
                }
            }
        }
    }
    cout << "Most occured element is: " <<  popular;
}
Meraj al Maksud
  • 1,528
  • 2
  • 22
  • 36
Arun Pandey
  • 513
  • 3
  • 6
  • 13

5 Answers5

12

Repeat solution twice and change two line.

if (count>max_count)
    max_count = count;

with:

if (count==max_count)
    cout << a[i] << endl;

Solution:

int a[5];
for (int i=0;i<5;i++)
   cin>>a[i];

int max_count = 0;

for (int i=0;i<5;i++)
{
   int count=1;
   for (int j=i+1;j<5;j++)
       if (a[i]==a[j])
           count++;
   if (count>max_count)
      max_count = count;
}

for (int i=0;i<5;i++)
{
   int count=1;
   for (int j=i+1;j<5;j++)
       if (a[i]==a[j])
           count++;
   if (count==max_count)
       cout << a[i] << endl;
}
hasan
  • 23,815
  • 10
  • 63
  • 101
3

To collect all answers and not just the first one, you may use std::vector<int> popular instead of int popular.

then when tempCount == count, popular.push_back(temp);,

when tempCount > count, popular.clear(); popular.push_back(temp);

Jarod42
  • 203,559
  • 14
  • 181
  • 302
1

I think this solution will work better and it is shorter.

#include <iostream>
using namespace std;

int main()
{

int topCount=0, count, topElement, array[10];

for (int i=0 ; i<10 ; i++)
{
    cin>>array[i];
}

for ( int i=0 ; i<10 ;i++)
{
    count=0;
    for (int j=0 ; j<10 ; j++)
    {
        if (array[i]==array[j]) count++;
    }
    if (count>topCount)
    {
        topCount=count;
        topElement=array[i];
    }
}

cout<<topElement<<" "<<topCount;
}
0

Here is a templatize solution:

template <class Iter, class ValType>
void findMostCommon_ (Iter first, Iter last)
{      
   typename std::vector<ValType> pop;
   int popular_cnt = 0;

   for (Iter it = first;   it != last;    ++it)
   {
      int temp_cnt = 0;

      for (Iter it2 = it + 1;  it2 != last;      ++it2)
         if (*it == *it2)
            ++temp_cnt;

      if (temp_cnt)
      {
         if (temp_cnt > popular_cnt)
         {
            popular_cnt = temp_cnt;
            pop.clear();
            pop.push_back(*it);
         }
         else if (temp_cnt == popular_cnt)
         {
            pop.push_back(*it);
         }
      }
   }

   if (pop.empty())  // all numbers unique
   {
      cout << "Could not find most popular" << endl;
   }
   else`enter code here`
   {
      cout << "Most popular numbers: ";

      for (typename std::vector<ValType>::const_iterator it = pop.begin(), lst = pop.end();   it != lst;    ++it)
         cout << (*it) << " ";
      cout << endl;
   }
}
0

Complete function for this type of problem:

int calcMode(int array[], int array_size)
{
int topCount=0, count, topElement = 10;

for ( int i=0 ; i<array_size ;i++)
{
    count=0;
    for (int j=0 ; j<array_size ; j++)
    {
        if (array[i]==array[j]) count++;
    }
    if (count>=topCount)
    {
        topCount=count;
        if (topElement > array[i])
            topElement=array[i];
    }
}

return topElement;
}