算法-寻找数组中的重复值

102 阅读1分钟
var arr=[1,2,4,4,3,3,1,5,3];
 
function res(arr){
 
var temp=[];
 
arr.forEach(function(item){
 
    if(arr.indexOf(item)!==arr.lastIndexOf(item)&&temp.indexOf(item)===-1){
    
          temp.push(item)
 
      }
 
    })
 
return temp;
 
}





function res(arr){
 
    var temp=[];
 
    arr.sort().sort(function(a,b){
 
       if(a===b&&temp.indexOf(a)===-1){
 
          temp.push(a)
 
      }
  
    })
  
    return temp;
}
 
var arr=[1,2,4,4,3,3,1,5,3];
 
console.log(res(arr))