js 一个数组各元素出现的次数,并输出,怎么写代码
2个回答
展开全部
<袭迟script>拍配李
var arr = [1, 5, 84, 4894, 44, 45, 454, 444, 515, 3, 4, 84, 4894, 44, 45, 454, 444, 515, 3, 4, 6, 6, 6, 6, 4];
for (var i = 0; i < arr.length; i++) {
if (arr.indexOf(arr[i]) == i) {
var num = 1;
for (var j = i + 1; j < arr.length; j++) {
if (arr[i] === arr[j]) {
num++;
}
}
console.log(arr[i] + "出现的次数是:" + num)
}
}
</script>卖卖
var arr = [1, 5, 84, 4894, 44, 45, 454, 444, 515, 3, 4, 84, 4894, 44, 45, 454, 444, 515, 3, 4, 6, 6, 6, 6, 4];
for (var i = 0; i < arr.length; i++) {
if (arr.indexOf(arr[i]) == i) {
var num = 1;
for (var j = i + 1; j < arr.length; j++) {
if (arr[i] === arr[j]) {
num++;
}
}
console.log(arr[i] + "出现的次数是:" + num)
}
}
</script>卖卖
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询