Skip to content

Instantly share code, notes, and snippets.

@skyl skyl/isSubSet.js
Created Apr 26, 2012

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

skyl commented Apr 26, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.