Skip to content

Commit

Permalink
Implement isSubsetOf.js
Browse files Browse the repository at this point in the history
  • Loading branch information
fay-jai committed Jan 4, 2015
1 parent a08db00 commit 3d113fc
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions isSubsetOf/isSubsetOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@
* including non-strings.
*/

Array.prototype.isSubsetOf = function(array){
// Your code here
};
Array.prototype.isSubsetOf = function (array) {
var subset = this;
var subsetLen = subset.length;
var supersetLen = array.length;
var first, found;

// base case
if ( subsetLen > supersetLen ) return false;
if ( subsetLen === 0 && supersetLen >= 0 ) return true;

// supLen is greater than or equal to subLen and supLen is greater than 0
first = subset[0];
found = array.indexOf( first );

if ( found === -1 ) return false;
return subset.slice(1).isSubsetOf( Array.prototype.concat( array.slice(0, found), array.slice(found + 1) ) );
};

0 comments on commit 3d113fc

Please sign in to comment.