Javascript Array isSubsetOf(arr)

Description

Javascript Array isSubsetOf(arr)


var b = ['merge','reset','reset']
Array.prototype.isSubsetOf = function (arr) {
   array=JSON.stringify(arr)//from  w  ww .  j  a v a2s .  com
   array2=JSON.stringify(this)
 if(array.includes(array2)){ return true }else{return false} 
}

Javascript Array isSubsetOf(arr)

/* //from   w  w w.  java 2  s. c  o  m
Make an array method that can return whether or not a context array is a subset of an input array.
*/

Array.prototype.isSubsetOf = function(arr) {
  var notAnArray = {};
  arr.forEach(function(e){
    notAnArray[e] = true;
  })
  for(var i = 0; i < this.length; i++){
    if(!notAnArray[this[i]]){
      return false;
    }
  }
  return true;
};

Javascript Array isSubsetOf(arr)

// Make an array method that can return whether or not a context array is a subset of an input array. To simplify the problem, you can assume that both arrays will contain only strings.

Array.prototype.isSubsetOf = function(arr) {
  return isSubsetObjs(this, objectify(arr));
};
var isSubsetObjs = function(arr, obj2) {
  for (var i = 0; i < arr.length; i++) {
    if (!obj2.hasOwnProperty(arr[i])) { 
     return false; 
    }//from w ww.j a v  a  2 s .  c o m
  } return true;
};
var objectify = function(arr) {
  return arr.reduce(function (out, item) {
    out[item] = true;
    return out;
  }, {})
};

Javascript Array isSubsetOf(arr)

//Slower time complexity due to nested loops
Array.prototype.isSubsetOf = function (arr) {
  for (var i = 0; i < this.length; i++) {
    if (arr.indexOf(this[i]) === -1) {
      return false;
    } /*from w  w w  .  j a v a  2s. co  m*/
  }
  return true;
}


//Linear time complexity
Array.prototype.isSubsetOf2 = function (arr) {
  //transfer arr to object for speed
  var obj = {};
  
    for (var i = 0; i < arr.length; i++) {
      obj[arr[i]] = arr[i];
    }
    for (var j = 0; j < this.length; j++) {
      if(!obj[this[j]]) {
        return false;
      }
    }
    return true;
}

var b = ['merge', 'reset' ,'reset']

b.isSubsetOf2(['reset','merge','add','commit', 'reset']) // true

Javascript Array isSubsetOf(arr)

Array.prototype.isSubsetOf = function (arr) {
 // your code here
 // Create an object to push all elements in the array into an object. 
 // var catalog = {}; 

 for (var i = 0; i < this.length; i++) {
  if (arr.indexOf(this[i]) === -1) {
   return false; 
  }//from  w  w w  . ja v a 2  s  .  com
 }
 return true; 
}; 

// var result = ['cat', 'dog', 'cow'].isSubsetOf(['dog', 'cow', 'fox']);
// console.log(result); // False

// var result = ['cat', 'cat', 'dog'].isSubsetOf(['cat', 'dog']); 
// console.log(result); // True

// var result = ['cat', 'cat', 'dog'].isSubsetOf(['cat']); 
// console.log(result); // False
// var b = ['merge','reset','reset']
// console.log(b.isSubsetOf(['reset','merge','add','commit'])) // true

Javascript Array isSubsetOf(arr)

/*/* ww  w.j  a  v a 2s . c  om*/
 * Make an array method that can return whether or not a context array is a
 * subset of an input array.  To simplify the problem, you can assume that neither
 * array will contain objects or arrays as elements within them.
 *
 *
 * var a = ['commit','push']
 * a.isSubsetOf(['commit','rebase','push','blame']) // true
 *
 * NOTE: You should disregard duplicates in the set.
 *
 * var b = ['merge','reset','reset']
 *
 * b.isSubsetOf(['reset','merge','add','commit']) // true
 *
 * See http://en.wikipedia.org/wiki/Subset for more on the definition of a
 * subset.
*/


/*
 * Extra credit: Make the method work for arrays that contain objects and/or arrays as elements.
*/

Array.prototype.isSubsetOf = function (arr) {
  for(let i of this){
    if(![].includes.call(arr, i)){
      return false;
    }
  }
  return true;

}
Array.prototype.isSubsetOf = function (arr) {

 
  for (var i=0;i<this.length;i++){
   if(arr.indexOf(this[i])===-1)
    return false;
  }
  return true;

}



PreviousNext

Related