Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an array is a subset of another array in JavaScript?

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(); 
like image 237
yangmei Avatar asked Aug 07 '16 06:08

yangmei


People also ask

How do you check if an array is subset of another array JS?

Naive Approach to Find whether an array is subset of another array. Use two loops: The outer loop picks all the elements of arr2[] one by one. The inner loop linearly searches for the element picked by the outer loop. If all elements are found then return 1, else return 0.

How do you check if an array is a subarray of another array?

Simple Approach: A simple approach is to run two nested loops and generate all subarrays of the array A[] and use one more loop to check if any of the subarray of A[] is equal to the array B[]. Efficient Approach : An efficient approach is to use two pointers to traverse both the array simultaneously.

What is a subset of an array?

A subset is defined as a set whose elements are all members of another set. We will input two arrays and check whether the second array is subset of first array and display accordingly.

How to know the elements in an array in JavaScript?

Insert into the set for the first array; that’s how we will know the elements in the array. Save the size of the array after inserting the first array element. Insert into the same set for the second array.

How to check whether a list is a subset of another list?

Any idea on how to check whether that list is a subset of another? List<double> t1 = new List<double> { 1, 3, 5 }; List<double> t2 = new List<double> { 1, 5 }; How to check that t2 is a subset of t1, using LINQ? If the lists are sorted (as in your example), this should be possible in O (n+m) time.

Is y[] a subset of X[]?

Given two unsorted arrays X [] and Y [] of size m and n, write a program to check whether Y [] is a subset of X [] or not. Y [] is a subset of X [] if each element of Y [] is available in X [].


1 Answers

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);
like image 183
simhumileco Avatar answered Oct 03 '22 05:10

simhumileco