Find the number of duplicates in two or more arrays

I’m trying to compare and find how many duplicates are there in two arrays.

const array1 = [a, b, c, d, e]
const array2 = [b, f, c, z, y]

let identical = 0

for (let i = 0; i < array1.length; i++) {
        if (array1[i] === array2[i]) {
          identical++
        }
      }

console.log(identical)
// returns 2

I did this but I want to use a shorter syntax (ES6 syntax)

8 thoughts on “Find the number of duplicates in two or more arrays”

Leave a Comment