Huawei Machine Test-the most frequently occurring elements in an integer array

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.