Skip to content

Instantly share code, notes, and snippets.

@surr-name
Last active September 15, 2015 21:16
Show Gist options
  • Save surr-name/1737d90cdc10256c5e86 to your computer and use it in GitHub Desktop.
Save surr-name/1737d90cdc10256c5e86 to your computer and use it in GitHub Desktop.
/**
* do the array b includes all elements from the array a
*
* @param {Array} a
* @param {Array} b
* @param {Boolean} [unsorted=false] are the arrays unsorted
*
* @returns {Boolean}
*/
function bIncludesA ( a, b, unsorted ) {
if ( unsorted ) {
a = a.sort();
b = b.sort();
}
var aPointer = bPointer = 0,
aLength = a.length,
bLength = b.length,
matches = 0;
while ( aPointer < aLength && bPointer < bLength ) {
if ( a[aPointer] === b[bPointer] ) {
matches++;
aPointer++;
bPointer++;
continue;
} else if ( a[aPointer] > b[bPointer] ) {
bPointer++;
continue;
}
break;
}
return aLength === matches;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment