Javascript Array isSubsetOf(input)

Description

Javascript Array isSubsetOf(input)


Array.prototype.isSubsetOf = function (input) {
  let set = new Set(input);
  return this.every(el => set.has(el));
}

Javascript Array isSubsetOf(input)

Array.prototype.isSubsetOf = function(input) {
  var result = true;
  // Iterate throough collection
  for (var i = 0; i < this.length; i++) {
    // Check if item in collection is also found in input
    if (input.indexOf(this[i]) === -1) {
      result = false;/*from w  w  w.  j  a  v a 2 s  . c om*/
    }
  }
  return result;
}

Javascript Array isSubsetOf(input)

// determines if an array is a subset of another array
Array.prototype.isSubsetOf = function(input) {
  var isSub = true;

  this.forEach(function (el) {
    if (!input.includes(el)) {
      isSub = false;/*from  w w  w . j  a va2 s.c  o  m*/
    }
  });
  return isSub;
};

Javascript Array isSubsetOf(input)

// Array.prototype.isSubsetOf = function(input){
//   //context.isSubsetOf(input);
//   var result = true;
//   this.forEach(function(item){
//     if(input.indexOf(item) !== -1){
//       //if item is inside input
//       result = result && true;
//     }else{//www .  j  av a  2s. c  om
//       result = false;
//     }
//   });
//   return result;
// }

Array.prototype.isSubsetOf = function(input){
  let result = true;
  this.forEach(item => input.indexOf(item) !== -1 ? result = result && true : result = false);
  return result;
}

Javascript Array isSubsetOf(input)

/*/*from  w  w  w  .ja  v  a  2 s  . co  m*/
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(input) {

  // iterate through this
  for (var i = 0; i < this.length; i++) {

    // check if this[i] exists in input
    if (input.indexOf(this[i]) === -1) {
      // if not, return false
      return false;
    }
    // if true, continue
  }
  return true;
};

Javascript Array isSubsetOf(input)

/*************************************************************
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(input) {
    //w ww  . j a  va2s  .  c  o  m
  var freq = {};
  for (var i = 0; i < this.length; i++) {
    freq[this[i]] = 1;
  }

  for (var j = 0; j < input.length; j++) {
    if (freq[input[j]]) {
      delete freq[input[j]];
    }
  }

  return Object.keys(freq).length === 0;
};



PreviousNext

Related