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();
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.
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.
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.
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.
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.
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 [].
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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With