Check Whether Elements from One Array Are Present in Another Array

Check whether elements from one array are present in another array

in terms of Algorithm Complexity wise , It's like trade-offs .
First One - Space time Complexity - 0, Run time complexity - 0(n2)

Second One - Space time Complexity - o(n), Run time complexity - 0(n)

If it's performance focussed , go for second one .

In terms of js way , you have many ways . Read about includes() and indexOf() inbuilt method in javascript to avoid writing a loop . Also make use of javascript map function . You could also uses underscore.js

Ref Check if an array contains any element of another array in JavaScript for more details .

Check if an array contains elements from another array

Your last example is correct if not for forgetting to return the value and moving the if outside.

const winConditions = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];

const squaresIndices = [0, 4, 8];

const isWin = winConditions.some(
(arr) =>
{
// return if this combination is matching or not
return arr.every(
(square) =>
{
// return if the value matches or not
return squaresIndices.includes(square);
}
);
}
);

// Then test the result
if (isWin)
{
alert("Game Over");
}

Check if an array contains 2 or more elements of another array in JavaScript

Firstly, you can use array#filter combined with array#includes to find all items in arr1 in arr2.

Then check the length of result.

let arr1 = [1, 2, 3];
let arr2 = [2, 3];

let result = arr1.filter(v1 => arr2.includes(v1));
console.log(result.length >= 2);

Check if an array of object is exactly equal to another array that contains one of its property

as @mplungjan mentiond, you can use Every:

let fixed = ["123", "456", "789"];
let variableArray1 = [{
name: "Joe",
id: "123"
}, {
name: "Joe",
id: "456"
}, {
name: "Joe",
id: "789"
}];
let variableArray2 = [{
name: "Joe",
id: "123"
}, {
name: "Joe",
id: "456"
}, {
name: "Joe",
id: "001"
}]


let containsAll1 = variableArray1.every(elem => fixed.includes(elem.id));
let containsAll2 = variableArray2.every(elem => fixed.includes(elem.id));

console.log(containsAll1, containsAll2);

Determine if one array contains all elements of another array, including any duplicates

const isMultiSubset = (target, value) => {
const occurences = new Map;
for(const entry of target)
occurences.set(entry, (occurences.get(entry) ?? 0) + 1);

for(const entry of value)
if (occurences.has(entry))
occurences.set(entry, occurences.get(entry) - 1);

return [...occurences.values()].every(count => count <= 0);
};

By using a Map to count occurences this can be solved in O(n + m).

JavaScript: How can i check if an array contains elements in common with a second array?

A quick solution could be something like this:

const pcNumbers = [1,2,3,8,5];
const userNumbers = [1,2,7,8,9];
const newArr = [];

for (let i = 0; i < pcNumbers.length; i++) {
for (let j = 0; j < userNumbers.length; j++) {
if (pcNumbers[i] === userNumbers[j]) {
newArr.push(pcNumbers[i])
}
}
};

console.log(`These are in common: ${newArr.join(', ')}`)

This will result in:

These are in common: 1, 2



Related Topics



Leave a reply



Submit