日期:2014-05-16 浏览次数:20372 次
var arr1=[1,2,3,4,5];
var arr2=[2,4,6,8];
var unix = new Array();
for (var i = 0; i < arr1.length; i++) {
if($.inArray(arr1[i] ,arr2) != -1){
unix.push(arr1[i]);
}
}
console.log(unix);
function intersection_destructive(a, b)
{
var result = new Array();
while( a.length > 0 && b.length > 0 )
{
if (a[0] < b[0] ){ a.shift(); }
else if (a[0] > b[0] ){ b.shift(); }
else /* they're equal */
{
result.push(a.shift());
b.shift();
}
}
return result;
}
var arr1=[1,2,3,4,5]
var arr2=[2,4,6,8]
alert(intersection_destructive(arr1,arr2));
var arr1=[1,2,3,4,5];
var arr2=[2,4,6,8];
var arr=arr1.concat(arr2);
arr.sort();
for(var i= 0,l=arr.length-1;i<l;i++){
if(arr[i]===arr[i+1]){
console.log(arr[i]);
i++;
}
}
var arr1=[1,2,3,4,5];
var arr2=[2,4,6,8];
;(","+arr1+",
------解决方案--------------------
,"+arr2+",").replace(/,(\d+)(?=,.*\
------解决方案--------------------
.*,\1,)/ig,function(s,t){
alert(t);
});
var arr1=[1,2,3,4,5];
var arr2=[2,4,6,8];
var ar =arr1.filter(function(n) {
return arr2.indexOf(n) != -1
});
alert(ar);