I am trying to solve this problem, you have to return an array of the items that are in both arrays given:
For example
console.log(findMatches([4,9,5], [9,4,9,8,4])) // returns [4,9] because 4 and 9 are in both arrays
My code already solves this first test, but the issue is, I have to return also the numbers x amount of times, of in other words *"Each element in the result should appear as many times as it shows in both arrays."
For example in
console.log(findMatches([1,2,2,1], [2,2])) // should return [2,2] because it is repetated in both arrays two times!!
That´s what i need to do now. Because this last test i don´t pass it. My function returns just [2] because it doesn´t count yet how many times is repeated the number. This is what I don´t know how to do.
function match(arr1, arr2){
let matchesArr = [];
arr1.map((item) => {
return arr2.map((item2) => {
if (item === item2){
matchesArr.push(item)
}
})
})
let filtered = [...new Set(matchesArr)]
return filtered
}
console.log(match([1,2,2,1], [2,2])); // WRONG should return [2,2], returns [2]
console.log(match([4,9,5], [9,4,9,8,4])); // CORRECT should return [4,9], returns [4,9]