-
-
Save binarymax/1193484 to your computer and use it in GitHub Desktop.
isSubset
This file contains hidden or 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( | |
S, //The Set array | |
u, //The Subset array to check | |
b, //Set index | |
s, //Subset index | |
e, //Set length | |
t //Subset length | |
) { | |
var A=S.slice().sort(), //copy and sort set array | |
B=u.slice().sort(); //copy and sort subset array | |
b=s=0; //init indexes | |
e=S.length; //get array lengths | |
t=u.length; | |
while(b<e&&s<t) //go until one array hits the end | |
if(A[b++]==B[s])s++; //if element found from subset in set, inc subset index | |
return s==t //returns true if every subset element exists | |
} |
This file contains hidden or 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(S,u,b,s,e,t){var A=S.slice().sort(),B=u.slice().sort();b=s=0;e=S.length;t=u.length;while(b<e&&s<t)if(A[b++]==B[s])s++;return s==t} |
This file contains hidden or 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 WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Max Irwin http://www.binarymax.com/ | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains hidden or 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
{ | |
"name": "isSubset", | |
"description": "Checks if array 'u' is subset of array 'S'.", | |
"keywords": [ | |
"subset", | |
"set" | |
] | |
} |
This file contains hidden or 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
<!DOCTYPE html> | |
<title>Foo</title> | |
<div>Expected value: <b>true,false,true,true</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
// write a small example that shows off the API for your example | |
// and tests it in one fell swoop. | |
var f = function(S,u,b,s,e,t){var A=S.slice().sort(),B=u.slice().sort();b=s=0;e=S.length;t=u.length;while(b<e&&s<t)if(A[b++]==B[s])s++;return s==t} | |
var a = ["apples","oranges","bananas","grapes","strawberries"]; | |
var b = ["strawberries","bananas"]; | |
var c = ["strawberries","kiwis"]; | |
var d = []; | |
var r1 = f(a,b); // b is a subset of a | |
var r2 = f(a,c); // c is not a subset of a | |
var r3 = f(a,d); // The empty set is a subset | |
var r4 = f(a,a); // A set is a subset of itself | |
document.getElementById( "ret" ).innerHTML = r1 + ',' + r2 + ',' + r3 + ',' + r4; | |
</script> |
Thanks for the info about ES5 Array.indexOf et al.
Btw, you mixed up the two arguments in your 63 bytes implementation. It should read:
function(a,b){return b.every(function(b){return~a.indexOf(b)})}
@p01 yap, I typo'ed and corrected it shortly after (before your comment on it). Reviewing the actual comment instead of the email notification will help prevent mixups like that.
Shortest non ES5-Fallback (tested against Jon's cases):
function(a,b,c,d,e,f){f=0;for(d in b){e=0;for(c in a)e+=a[c]===b[d];f+=e}return!!f}
83bytes, tested in FF6, probable issues with for...in
in some engines, yet untested.
Using Array#indexOf
or ===
will fail on a = [NaN], b = [NaN]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice one, though I'd still prefer it to be alongside a solution that does not require the browser to be ES5-compliant.