Skip to content

Instantly share code, notes, and snippets.

@abozhilov
Created August 12, 2013 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abozhilov/6209657 to your computer and use it in GitHub Desktop.
Save abozhilov/6209657 to your computer and use it in GitHub Desktop.
Array pull inplace
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