Last active
September 15, 2015 21:16
-
-
Save surr-name/1737d90cdc10256c5e86 to your computer and use it in GitHub Desktop.
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
/** | |
* 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