Most occurring element in an array using c++?

巧了我就是萌 提交于 2021-02-07 03:51:27

问题


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;
}

回答1:


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;
}



回答2:


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);




回答3:


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;
   }
}



回答4:


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;
}



回答5:


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;
}


来源:https://stackoverflow.com/questions/19210001/most-occurring-element-in-an-array-using-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!