Home > Web Front-end > JS Tutorial > js alternative method of non-repeating arrangement of pure numbers_javascript skills

js alternative method of non-repeating arrangement of pure numbers_javascript skills

WBOY
Release: 2016-05-16 18:22:46
Original
1064 people have browsed it

After referring to the Wuyou cosin method (thank you), the finishing method is as follows

Copy the code The 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 = [];
//Use numbers as subscripts and values Put it into another array to achieve sorting and non-repetition
for (var i =0;iif(numSubscript[baseNum[i]] == undefined)numSubscript[baseNum[i ]]=baseNum[i];
}
//Remove empty values ​​and reverse them
baseNum = numSubscript.join(',').replace(/([,] )/ig, ' ,').split(',').reverse();
document.write(baseNum);

Off topic:
If the array is generated by yourself, customer service will write This code is just fine. (Awesome)
Copy code The 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(''));
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template