任意长度的数组被删除的元素


一个数组arr0,长度任意,删除其中某个元素后得到数组arr,求删除的元素?

JavaScript 算法

豊総耳机子 12 years ago
   
  function array_diff (arr1) {
  
var retArr = [],argl = arguments.length,k1 = '',i = 1,k = '',arr = {};
arr1keys: for (k1 in arr1) {
for (i = 1; i < argl; i++) {
arr = arguments[i];
for (k in arr) {
if (arr[k] === arr1[k1]) {
// If it reaches here, it was found in at least one array, so try next value
continue arr1keys;
}
}
retArr.push(arr1[k1]);
}
}

return retArr;
}

var arr0 = [1,3,4,5];
var arr = [1,3,5];
alert(array_diff(arr0,arr));

早安I.st answered 12 years ago

Your Answer