The top 10 most frequently occurring numbers in the PHP numeric array (TOPK problem)

Source: Internet
Author: User
TOPK category, used to count the frequency of occurrence of words, report, and data statistics! No random value array is generated for ($ i0; $ i1000; $ I ++) {$ ary [] rand () ;}$ aryarray_count_values ($ ary ); count the number of occurrences of all values in the array. arsort ($ ary); sort in reverse order $ i1; foreach ($ aryas $ key $ value ){

TOPK category, used to count the frequency of occurrence of words, report, and data statistics! No // randomly generated value array for ($ I = 0; $ i1000; $ I ++) {$ ary [] = rand );} $ ary = array_count_values ($ ary); // count the number of times all values in the array appear. arsort ($ ary); // sort in reverse order $ I = 1; foreach ($ ary as $ key = $ value ){

TOPK category, used to count the frequency of occurrence of words, report, and data statistics! <无>
// Randomly generate a value array for ($ I = 0; $ I <1000; $ I ++) {$ ary [] = rand );} $ ary = array_count_values ($ ary); // count the number of times all values in the array appear. arsort ($ ary); // sort in reverse order $ I = 1; foreach ($ ary as $ key = >$ value) {if ($ I <= 10) {printf ("number: % d appears % d in total
", $ Key, $ value) ;}else {break ;}$ I ++;} unset ($ ary); // result number: 255 6 times in total: 443: 5 times in total: 906: 5 times in total: 623: 5 times in total: 586: 4 times in total: 660: 4 times in total: 873 appear 4 times: 208 appear 4 times: 247 appear 4 times: 240 appear 4 times: appear 4 times

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.