方法一:
<script>
var array = ['1','2','5','1','4','4','2','3','5','1','1','5','','',
''];
var arr = new Array();
var test = new Array();
var num = 1;
var temp = "";
var size = array.length;
console.log("-----------------------------"+size);
for(var i=0;i<size;i++){
for(var j=i+1;j<size;j++){
if(array[i] == array[j]){
temp = array[j];
array[j] = array[size-1];
array[size-1] = temp;
size--;
j--;
num++;
}
}
test[i] = num;
num = 1;
arr[i] = array[i]
}
for(var i=0;i<arr.length;i++){
console.log("----------------------元素:"+arr[i]+"\t\t个数为:"+test[i]);
}
</script>
方法二:
<script>
var array = new Array(1,2,5,1,4,4,2,3,5,1,1,5,5,5,6,7,3,9,9,10);
var arr = new Array();
var test = new Array();
var num = 1;
var temp = "";
var size = array.length;
document.write("-----------------------------"+size+'<br>');
for(var i=0;i<size;i++){
for(var j=i+1;j<size;j++){
if(array[i] == array[j]){
array.splice(j,1)
size--;
j--;
num ++;
}
}
test[i] = num ;
num = 1;
arr[i] = array[i]
}
for(var i=0;i<arr.length;i++){
document.write("----------------------元素:"+arr[i]+"个数为:"+test[i]+'<br>');
}
</script>
转载于:https://blog.51cto.com/11990649/1873314