Skip to content

Instantly share code, notes, and snippets.

@binarymax
Forked from 140bytes/LICENSE.txt
Created September 4, 2011 20:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save binarymax/1193484 to your computer and use it in GitHub Desktop.
Save binarymax/1193484 to your computer and use it in GitHub Desktop.
isSubset
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
}
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}
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.
{
"name": "isSubset",
"description": "Checks if array 'u' is subset of array 'S'.",
"keywords": [
"subset",
"set"
]
}
<!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>
@jdalton
Copy link

jdalton commented Sep 10, 2011

@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.

@atk
Copy link

atk commented Sep 13, 2011

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.

@FarSeeing
Copy link

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