79

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
  • 31,877
  • 16
  • 137
  • 115
yangmei
  • 813
  • 2
  • 7
  • 8

9 Answers9

150

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
  • 31,877
  • 16
  • 137
  • 115
  • 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,851
  • 3
  • 44
  • 39
  • 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
10

You can use this simple piece of code.

PlayerOne.every(function(val) { return PlayerTwo.indexOf(val) >= 0; })
simhumileco
  • 31,877
  • 16
  • 137
  • 115
SuperNova
  • 25,512
  • 7
  • 93
  • 64
10
function isSubsetOf(set, subset) {
    return Array.from(new Set([...set, ...subset])).length === set.length;
}
Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
Armen Zakaryan
  • 1,037
  • 1
  • 9
  • 7
  • 12
    `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
  • 25,512
  • 7
  • 93
  • 64
  • 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
2

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
  • 129
  • 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,666
  • 2
  • 35
  • 41
0

Here's a better example, that covers cases with duplicates in the subset array:

function isArraySubset(source, subset) {
  if (subset.length > source.length) return false;

  const src = [...source]; // copy to omit changing an input source
  for (let i = 0; i < subset.length; i++) {
    const index = src.indexOf(subset[i]);
    if (index !== -1) {
      src.splice(index, 1);
    } else {
      return false;
    }
  }
  return true;
}

console.log(isArraySubset(['b', 'c', 'a', 'd'], ['d', 'c'])); // true
console.log(isArraySubset(['b', 'c', 'a', 'd'], ['d', 'c', 'c'])); // false
  • By definition a set does not contain duplicates. So if it would - as you state in your answer, the items would be by their index and your answer wrong. And if it would not be, your name it as well already. – hakre Jan 28 '23 at 13:36
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33697614) – hakre Jan 28 '23 at 13:36
-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
  • 239
  • 2
  • 10