Huawei trial-the most frequently-used element in the integer array, Huawei integer

Source: Internet
Author: User

Huawei trial-the most frequently-used element in the integer array, Huawei integer

Retrieve the elements that appear most frequently in an integer array and return them in ascending order.


Implementation Method:

Public static int [] calcTimes (int [] num, int len );

[Input] num: an integer array;

Len: Number of Integers

[Return] returns the most frequently-occurring elements in an integer array in ascending order.
[Note] You only need to complete the function algorithm, and there is no IO input or output in the middle.

Example
Input: num = {, 3, 4, 4, 9, 9, 10} len = 10

Return Value: {4, 9}


# Include <iostream> using namespace std; int compare (const void * p, const void * q) {return * (int *) p-* (int *) q ;} // The number size cannot exceed 10 void 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 be incomplete. Check for missing information:



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.