An alternative method for js pure numbers not to be arranged repeatedly

Source: Internet
Author: User

After referring to the carefree cosin method (thanks), the solution is as follows:
Copy codeThe Code is as follows:
// A random number, used for Simulation
Var baseNum = [];
For (var I = 0; I <100000; I ++ ){
Random = Math. floor (Math. random () * I );
BaseNum. push (random );
}
Var baseNumLen = baseNum. length;
Var numSubscript = [];
// Place numbers as subscripts and values in another array for sorting and non-repetition
For (var I = 0; I <basenumlen; I ++ ){
If (numSubscript [baseNum [I] = undefined) numSubscript [baseNum [I] = baseNum [I];
}
// Remove the null value and reverse it.
BaseNum = numSubscript. join (','). replace (/([,] +)/ig, ','). split (','). reverse ();
Document. write (baseNum );

Question:
If the array is self-generated, the code written in the result of the customer service is very good. (AWESOME)
Copy codeThe Code is as follows:
Var baseNum = [], tmp = {}, v;
For (var I = 0; I <100000; I ++ ){
Tmp [Math. floor (Math. random () * I)] = true;
};
I = 0;
For (var k in tmp ){
BaseNum [I ++] = k;
};
BaseNum. sort (fn );
Function fn (x, y ){
Return x-y> 0? 1:-1
}
Document. write (baseNum. join (''));

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.