
废话不多说上代码:
function find_most(arr){
var keyMap={};
var i,len=arr.length,count=0,count_item;
for(i=0;i<len;i++){
var item=arr[i];
if(!keyMap[item]){
keyMap[item]=1;//判断这个对象里有没有这个以这个元素名为属性的元素
}
else{//去重也可以用类似的方法
keyMap[item]+=1;
}
if(keyMap[item]>count){
count=keyMap[item];
count_item=item;
}
}
console.log('出现最多的是',count_item,'出现了',count,'次');
return count_item;
}
find_most([1,1,2,2,2,13]);
function return_index(item,arr){
var new_array=arr.reverse();
return arr.length-1-new_array.indexOf(item);
}//自己想出来的先把数组倒过来,然后就简单了
console.log(return_index(find_most([1,1,2,2,2,13]),[1,1,2,2,2,13]));




近期评论