Nodejs Array Subset isSubsetOf(arr)

Here you can find the source of isSubsetOf(arr)

Method Source Code

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; 
      }/*w ww  .ja  v a2  s . co m*/
   }
   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

Related

  1. isSubsetOf()
    Array.prototype.isSubsetOf = function() {
        var args=arguments[0];
        var len=this.length;
        var i;
        var counter=0;
        console.log(args);
        for(i=0; i<len; i++){
            if(args.indexOf(this[i])>=0){
                counter++;
    ...
    
  2. isSubsetOf(arr)
    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; 
      } return true;
    ...
    
  3. isSubsetOf(arr)
    Array.prototype.isSubsetOf = function (arr) {
      var objectify = function(arr) {
        var obj = {};
        for (var i = 0; i < arr.length; i++) {
          obj[arr[i]] = true;
        return obj;
      };
      var obj = objectify(arr);
    ...
    
  4. isSubsetOf(arr)
    Array.prototype.isSubsetOf = function (arr) {
      let result = 0;
      for (let i = 0; i < this.length; i += 1) {
        for (let p = 0; p < arr.length; p += 1) {
          if (this[i] === arr[p]) {
            result += 1;
            break;
        if (i + 1 !== result) {
          return false;
      return true;
    };
    const b = ['merge','reset','reset']
    console.log(b.isSubsetOf(['reset','merge','add','commit']));
    const a = ['commit','push','blame'];
    console.log(a.isSubsetOf(['commit','rebase','push','blame']));
    
  5. isSubsetOf(arr)
    Array.prototype.isSubsetOf = function (arr) {
      var subArr = this;
      var words = {};
      for (var i = 0; i < arr.length; i++) {
        if (!words[arr[i]]) {
          words[arr[i]] = true;
      for (var i = 0; i < subArr.length; i++) {
    ...
    
  6. isSubsetOf(arr)
    Array.prototype.isSubsetOf = function (arr) {
      var objectify = function (arr) {
        var obj = {};
        for (var i = 0; i < arr.length; i++) {
          obj[arr[i]] = true;
        return obj;
      };
      var obj = objectify(arr);
    ...