Skip to content

Instantly share code, notes, and snippets.

@heroicyang
Created May 17, 2013 12:15
Show Gist options
  • Save heroicyang/5598663 to your computer and use it in GitHub Desktop.
Save heroicyang/5598663 to your computer and use it in GitHub Desktop.
数组去重(uniq)
function uniq (arr1, arr2) {
var result = arr1.concat(arr2)
, len = result.length;
for (var i = 0; i < len; i++) {
var firstIdx = result.indexOf(result[i])
, lastIdx = result.lastIndexOf(result[i]);
while (firstIdx !== lastIdx) {
result.splice(lastIdx, 1);
len = len - 1;
firstIdx = result.indexOf(result[i]);
lastIdx = result.lastIndexOf(result[i]);
}
}
return result;
}
var a = [1, 10, 5, 8, 4]
, b = [5, 2, 5, 7, 84, 1];
console.log(uniq(a, b)); // [ 1, 10, 5, 8, 4, 2, 7, 84 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment