Nodejs Array Subset isSubsetOfarray()

Here you can find the source of isSubsetOfarray()

Method Source Code

/*/*www.  j av  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.
 *
 * 
 * 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 any value,
 * including non-strings.
*/

Array.prototype.isSubsetOf = array => {
  let hash = {};
  let result = true;
  array.forEach(item => {
    hash.item ? hash.item++ : hash.item = 0;
  });
  this.forEach(item => {
    hash.item ? hash.item++ : result = false;
  });
  return result;
};

Related

  1. isSubsetOf(input)
    Array.prototype.isSubsetOf = function(input) {
      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;
    };
    
  2. isSubsetOf(inputArray)
    Array.prototype.isSubsetOf = function(inputArray) {
      var exists = true;
      for (var i = 0; i < this.length; i++) {
        if (inputArray.indexOf(this[i]) === -1) {
          exists = false;
      return exists;
    };
    ...
    
  3. isSubsetOf(inputArray)
    Array.prototype.isSubsetOf = function(inputArray) {
      for (var i = 0; i < this.length; i++) {
        var contains = false;
        for (var j = 0; j < inputArray.length; j++) {
          if (inputArray[j] === this[i]) {
            contains = true;
        if (!contains) {
    ...
    
  4. isSubsetOf(matchArr)
    Array.prototype.isSubsetOf = function (matchArr) {
      var arr = this;
      for (var i = 0; i < arr.length; i++) {
        if (matchArr.indexOf(arr[i]) === -1 && typeof arr[i] !== 'object') {
          return false;
      return true;
    };
    ...
    
  5. isSubsetOf(subset)
    'use strict';
    Array.prototype.isSubsetOf = function(subset){
      var arr = this;
    };