Created
November 4, 2014 08:38
-
-
Save KnightAlex/6bce6da71697fb30aacc to your computer and use it in GitHub Desktop.
Find one arrays items in another array
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 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