Comparing Two Arrays Containing Integers Javascript
I'm trying to compare two arrays. firstArray has 451 integers in it, secondArray has 91 integers in it. All integers that are in secondArray are also in firstArray. I want to log o
Solution 1:
Use flags to keep track of which parts are in the set and which are not.
const arrOne = [34,2,12,4,76,7,8,9]
const arrTwo = [8,12,2]
let isInSet = false
const notInSet = []
// Expected output: 34, 4, 76, 7, 9
arrOne.forEach((el, i) => {
isInSet = false
arrTwo.forEach((curr, j) => {
if (el == curr) {
isInSet = true
}
})
if (!isInSet) {
notInSet.push(el)
}
})
console.log(notInSet)
// OR spread them out
console.log(...notInSet)
Post a Comment for "Comparing Two Arrays Containing Integers Javascript"