Created
May 17, 2013 12:15
-
-
Save heroicyang/5598663 to your computer and use it in GitHub Desktop.
数组去重(uniq)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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