var arr = '01,08,16,27,34,04,11,19,25,32,03,18,19,26,34,01,05,07,22,25,03,05,13'
arr = arr.split(',').sort(); //先将得到的数据进行排序;
var newArr = []; //
for (var i = 0; i < arr.length;) {
var count = 0;
for (var j = i; j < arr.length; j++) {
if (arr[i] == arr[j]) {
count++;
}
}
newArr.push({
'Number': arr[i],
'count':count
});
i += count
}
结果如下:
鉴于方便观察,将数据根据count进行排序;
function bubbleSort(arr) { var len = arr.length; for (var i = 0; i < len; i++) { for (var j = 0; j < len - 1 - i; j++) { if (arr[j].count > arr[j + 1].count) { //相邻元素两两对比 var temp = arr[j + 1]; //元素交换 arr[j + 1] = arr[j]; arr[j] = temp; } } } return arr; }
得到结果: