Remove the most frequently occurring elements in an integer array and return in ascending order.
Required Implementation method:
public static int[] Calctimes (int[] num, int len);
"Input" num: integer array;
Len: number of integers entered
"Return" in ascending order returns the most frequently occurring elements in an array of integers
"Note" Only needs to complete the function algorithm, the middle does not need any IO input and output
Example
Input: num = {1,1,3,4,4,4,9,9,9,10} len = 10
return: {4,9}
#include <iostream> #include <map> #include <vector> #include <algorithm>using namespace std; int main (int argc, char *argv[]) { int num[]={1,1,3,4,4,4,9,9,9,10}; int len=sizeof (num)/sizeof (int); map<int,int>m; vector<int>vec; for (int i=0;i<len;++i) m[i]=0; int cnt=0; for (int i=0;i<len;++i) { m[num[i]]++; if (m[num[i]]>cnt) Cnt=m[num[i]]; } For (Map<int,int>::iterator It=m.begin (); It!=m.end (); ++it) { if (it->second==cnt) Vec.push_back (It->first); } Sort (Vec.begin (), Vec.end ()); For (Vector<int>::iterator It=vec.begin (); It!=vec.end (); ++it) { cout<<*it<<endl; } return 0;}
Test results:
Huawei Machine Test-the most frequently occurring elements in an integer array