69

Let's say I have two arrays,

var PlayerOne = ['B', 'C', 'A', 'D'];
var PlayerTwo = ['D', 'C'];

What is the best way to check if arrayTwo is subset of arrayOne using javascript?

The reason: I was trying to sort out the basic logic for a game Tic tac toe, and got stuck in the middle. Here's my code anyway... Thanks heaps!

var TicTacToe = {


  PlayerOne: ['D','A', 'B', 'C'],
  PlayerTwo: [],

  WinOptions: {
      WinOne: ['A', 'B', 'C'],
      WinTwo: ['A', 'D', 'G'],
      WinThree: ['G', 'H', 'I'],
      WinFour: ['C', 'F', 'I'],
      WinFive: ['B', 'E', 'H'],
      WinSix: ['D', 'E', 'F'],
      WinSeven: ['A', 'E', 'I'],
      WinEight: ['C', 'E', 'G']
  },

  WinTicTacToe: function(){

    var WinOptions = this.WinOptions;
    var PlayerOne = this.PlayerOne;
    var PlayerTwo = this.PlayerTwo;
    var Win = [];

    for (var key in WinOptions) {
      var EachWinOptions = WinOptions[key];

        for (var i = 0; i < EachWinOptions.length; i++) {
          if (PlayerOne.includes(EachWinOptions[i])) {
            (got stuck here...)
          }

        }
        // if (PlayerOne.length < WinOptions[key]) {
        //   return false;
        // }
        // if (PlayerTwo.length < WinOptions[key]) {
        //   return false;
        // }
        // 
        // if (PlayerOne === WinOptions[key].sort().join()) {
        //   console.log("PlayerOne has Won!");
        // }
        // if (PlayerTwo === WinOptions[key].sort().join()) {
        //   console.log("PlayerTwo has Won!");
        // } (tried this method but it turned out to be the wrong logic.)
    }
  },


};
TicTacToe.WinTicTacToe();
simhumileco
  • 27,137
  • 16
  • 123
  • 105
yangmei
  • 713
  • 2
  • 6
  • 8

8 Answers8

127

Here is the solution:

Using ES7 (ECMAScript 2016):

const result = PlayerTwo.every(val => PlayerOne.includes(val));

Snippet:

const PlayerOne = ['B', 'C', 'A', 'D'];
const PlayerTwo = ['D', 'C'];

const result = PlayerTwo.every(val => PlayerOne.includes(val));

console.log(result);

Using ES5 (ECMAScript 2009):

var result = PlayerTwo.every(function(val) {

  return PlayerOne.indexOf(val) >= 0;

});

Snippet:

var PlayerOne = ['B', 'C', 'A', 'D'];
var PlayerTwo = ['D', 'C'];

var result = PlayerTwo.every(function(val) {

  return PlayerOne.indexOf(val) >= 0;

});

console.log(result);

Here is answer the question at the comment below:

How do we handle duplicates?

Solution: It is enough to add to the above solution, the accurate condition for checking the number of adequate elements in arrays:

const result = PlayerTwo.every(val => PlayerOne.includes(val) 
    && PlayerTwo.filter(el => el === val).length
       <=
       PlayerOne.filter(el => el === val).length
);

Snippet for first case:

const PlayerOne = ['B', 'C', 'A', 'D'];
const PlayerTwo = ['D', 'C'];

const result = PlayerTwo.every(val => PlayerOne.includes(val) 
    && PlayerTwo.filter(el => el === val).length
       <=
       PlayerOne.filter(el => el === val).length
);

console.log(result);

Snippet for second case:

const PlayerOne = ['B', 'C', 'A', 'D'];
const PlayerTwo = ['D', 'C', 'C'];

const result = PlayerTwo.every(val => PlayerOne.includes(val) 
    && PlayerTwo.filter(el => el === val).length
       <=
       PlayerOne.filter(el => el === val).length
);

console.log(result);
simhumileco
  • 27,137
  • 16
  • 123
  • 105
  • 1
    How do we handle duplicates? For example: var PlayerOne = ['B', 'C', 'A', 'D']; var PlayerTwo = ['D', 'C']; The above comparison should result in **true** var PlayerOne = ['B', 'C', 'A', 'D']; var PlayerTwo = ['D', 'C', 'C']; The above comparison should result in **false** – Nrupesh Aug 31 '20 at 13:34
  • 2
    I just expanded the answer with the answer to your great question @Nrupesh. Enjoy! :) – simhumileco Aug 31 '20 at 16:38
21

If you are using ES6:

!PlayerTwo.some(val => PlayerOne.indexOf(val) === -1);

If you have to use ES5, use a polyfill for the some function the Mozilla documentation, then use regular function syntax:

!PlayerTwo.some(function(val) { return PlayerOne.indexOf(val) === -1 });
tothemario
  • 5,131
  • 3
  • 38
  • 36
  • 2
    Even better: `!PlayerTwo.some(val => !PlayerOne.includes(val));` – Terry Mar 04 '19 at 15:08
  • 3
    Using [every](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every) is better than the inverted some,`every` will short circuit if an element is false, so it would take the same time, and it's a lot more readable. – Amr Saber Aug 20 '19 at 10:01
9

You can use this simple piece of code.

PlayerOne.every(function(val) { return PlayerTwo.indexOf(val) >= 0; })
simhumileco
  • 27,137
  • 16
  • 123
  • 105
SuperNova
  • 21,204
  • 6
  • 80
  • 55
9
function isSubsetOf(set, subset) {
    return Array.from(new Set([...set, ...subset])).length === set.length;
}
Pika Supports Ukraine
  • 3,394
  • 9
  • 24
  • 41
Armen Zakaryan
  • 923
  • 1
  • 8
  • 6
  • 7
    `Array.from(...)` is not necessary. Use `.size` of the new set directly: `new Set([...set, ...subset]).size` – Fabian Oct 31 '19 at 10:31
7

If PlayerTwo is subset of PlayerOne, then length of set(PlayerOne + PlayerTwo) must be equal to length of set(PlayerOne).

var PlayerOne = ['B', 'C', 'A', 'D'];
var PlayerTwo = ['D', 'C'];

// Length of set(PlayerOne + PlayerTwo) == Length of set(PlayerTwo)

Array.from(new Set(PlayerOne) ).length == Array.from(new Set(PlayerOne.concat(PlayerTwo)) ).length
SuperNova
  • 21,204
  • 6
  • 80
  • 55
  • 1
    What about duplications? Is `['B', 'B']` a subset of `['B', 'C', 'A', 'D']`? Or just for `['B', 'B', 'C', 'A', 'D']`? The above logics did not handle these cases. – gazdagergo Nov 27 '19 at 10:20
1

If you want to compare two arrays and take also order under consideration here is a solution:

  let arr1 = [ 'A', 'B', 'C', 'D' ];
  let arr2 = [ 'B', 'C' ];
  arr1.join().includes(arr2.join()); //true

  arr2 = [ 'C', 'B' ];
  arr1.join().includes(arr2.join()); //false
Piotr Gajek
  • 119
  • 3
0

Here is a solution that exploits the set data type and its has function.

let PlayerOne = ['B', 'C', 'A', 'D', ],
    PlayerTwo = ['D', 'C', ],
    [one, two] = [PlayerOne, PlayerTwo, ]
        .map( e => new Set(e) ),
    matches = Array.from(two)
        .filter( e => one.has(e) ),
    isOrisNot = matches.length ? '' : ' not',
    message = `${PlayerTwo} is${isOrisNot} a subset of ${PlayerOne}`;
console.log(message)

Out: D,C is a subset of B,C,A,D
dmmfll
  • 2,536
  • 2
  • 33
  • 40
-1

This seems most clear to me:

function isSubsetOf(set, subset) {
    for (let i = 0; i < set.length; i++) {
        if (subset.indexOf(set[i]) == -1) {
            return false;
        }
    }
    return true;
}

It also has the advantage of breaking out as soon as a non-member is found.

Sanford Staab
  • 209
  • 2
  • 9