Created
August 12, 2013 10:05
-
-
Save abozhilov/6209657 to your computer and use it in GitHub Desktop.
Array pull inplace
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 pull(arr) { | |
var map = {}, | |
len = arr.length, | |
args = arguments; | |
for (var i = 1, argsLen = args.length; i < argsLen; i++) { | |
for (var j = len; j--;) { | |
if (arr[j] === args[i]) { | |
map[j] = true; | |
} | |
} | |
} | |
for (var i = 0, k = 0; i < len; i++) { | |
if (!map[i]) { | |
arr[k++] = arr[i]; | |
} | |
} | |
arr.length = k; | |
return arr; | |
} | |
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | |
console.log(pull(arr, 3, 2, 9, 4, 5)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment