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>using namespace Std;int compare (const void *p,const void *q) {return * (int *) p-* (int *) Q;} The number size does not exceed 10void calctimes (int num[],int len) {qsort (num,len,sizeof (int), compare); int count[11]={0};for (int i=0;i< len;i++) Count[num[i]]++;int max = Count[0];for (i=0;i<11;i++) if (Count[i]>max) max=count[i];for (i=0;i<11;i+ +) if (Count[i]==max) cout<<i<< ""; Cout<<endl;} int main () {int n,i=0,a[100];cin>>n;while (GetChar ()! = ' \ n ') {a[i++]=n;cin>>n;} A[i++]=n;calctimes (a,i); return 0;}
Test results, may not be thoughtful, welcome to check the leak:
Huawei Machine Test-the most frequently occurring elements in an integer array