Front-end written questions practice notes: Statistics The probability of the occurrence of different numbers in an array and sorts

Source: Internet
Author: User
Tags sorts

title: There is an int array with several numbers in it. How many different numbers are required to be counted? How often are each number appearing? The array is arranged from less to more frequency, the same frequency is from small to large.
Workaround: Use an array to store different numbers and occurrence probabilities, compare the numbers in each group with the other numbers in the group, and set the same one to NULL, as well as the number of occurrences of the numbers plus 1, and finally use the bubble sort
JS Implementation code:

varnumberdata=[1,4,5,7,8,3,1,6,4,8,4,6,9,410, A, the, A,6, -, A,5,7,8,5,2,4,1,5,6,3,Ten]; function countnumberarray(){    varResult=[]; result[0]=[]; result[1]=[];varalldisnum=0;varindex=0;//Statistics of different numbers     for(vari = numberdata.length-1; I >=0; i--) {varTemp=numberdata[i];if(temp) {result[0][index]=temp; result[1][index]=1; for(varj = numberdata.length-1; J >=0; j--) {if(temp==numberdata[j]&&j!=i) {result[1][index]+=1; numberdata[j]=NULL;        }} index++; }    }//Sort     for(vari =0; I <result[1].length-1; i++) { for(varj=i+1; j<result[1].length;j++) {varfcnt=result[1][i];varfnum=result[0][i];if(fcnt>result[1][j]| (fcnt==result[1][j]&&fnum>result[0][J])) {result[1][i]=result[1][J]; result[1][j]=fcnt; result[0][i]=result[0][J]; result[0][j]=fnum; }        }    }}

Front-end written questions practice notes: Statistics The probability of the occurrence of different numbers in an array and sorts

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.