使用 C++ 的数组中出现次数最多的元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19210001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Most occurring element in an array using c++?
提问by Arun Pandey
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;
}
回答by hasan
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;
}
回答by Jarod42
To collect all answers and not just the first one,
you may use std::vector<int> popular
instead of int popular
.
要收集所有答案而不仅仅是第一个答案,您可以使用std::vector<int> popular
代替int popular
。
then when tempCount == count
, popular.push_back(temp);
,
那么当tempCount == count
, popular.push_back(temp);
,
when tempCount > count
, popular.clear(); popular.push_back(temp);
当tempCount > count
,popular.clear(); popular.push_back(temp);
回答by Ali Kayaten
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;
}
}
回答by Kristians Murds
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;
}
回答by Drift Junkie
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;
}