Skip to content

Instantly share code, notes, and snippets.

@skyl
Created April 26, 2012 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skyl/2501981 to your computer and use it in GitHub Desktop.
Save skyl/2501981 to your computer and use it in GitHub Desktop.
Javascript array, is Sub Set
isSubSet = function(ar1, ar2) {
// Test if ar1 is a subset of ar2
ar1 = _.uniq(ar1);
ar2 = _.uniq(ar2);
if (ar1.length > ar2.length) {
return false;
}
_.each(ar1, function(el){
if (ar2.indexOf(el) === -1){
return false;
}
});
return true;
}
ar1 = [1,2,3];
ar2 = [1,2];
ar3 = [1, 2, "foo"];
b = ["a", "b", "c"];
ar4 = [2, b];
ar5 = [b];
//alert(isSubSet(ar5, ar4));
//alert(isSubSet(ar5, ar5));
//alert(isSubSet(ar4, ar5));
//res = isSubSet(ar1, ar2);
//alert(res);
//res = isSubSet(ar2, ar1);
//alert(res);
//res = isSubSet(ar3, ar2);
//alert(res);
//res = isSubSet(ar2, ar3);
//alert(res);
z = [["foo", "bar"], b];
x = [["foo", "bar"], ["a", "b", "c"], 5];
//alert(isSubSet(z, x));
// BAD CASES ??
//alert(isSubSet([], [1,2,3])); //true - I think this is right.
@skyl
Copy link
Author

skyl commented Apr 26, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment