Skip to content

Instantly share code, notes, and snippets.

@KnightAlex
Created November 4, 2014 08:38
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 KnightAlex/6bce6da71697fb30aacc to your computer and use it in GitHub Desktop.
Save KnightAlex/6bce6da71697fb30aacc to your computer and use it in GitHub Desktop.
Find one arrays items in another array
function compareArrays(sup, sub) {
sup.sort();
sub.sort();
var i, j;
for (i=0,j=0; i<sup.length && j<sub.length;) {
if (sup[i] < sub[j]) {
++i;
} else if (sup[i] == sub[j]) {
++i; ++j;
} else {
// sub[j] not in sup, so sub not subbag
return false;
}
}
// make sure there are no elements left in sub
return j == sub.length;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment