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)

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

  1. reduce can count up the number of matches pretty concisely:

    const identical = array1.reduce(
      // on each iteration, return the prior accumulator value
      // (matches found so far)
      // plus one, if the current iteration is a match, otherwise plus zero
      (a, num, i) => a + (num === array2[i]),
      0 // initial value
    );
    
    Reply
  2. If you want to count only same items per index, you can use .reduce as follows:

    const array1 = ['a', 'b', 'c', 'd', 'e'];
    const array2 = ['b', 'f', 'c', 'z', 'y'];
    
    const identical = array1.reduce((acc, item, index) => 
      array2[index]===item ? acc+1 : acc
    , 0);
    
    console.log(identical);
    Reply
  3. reduce() is the fit for purpose method, but you can also achieve this by using filter() and accessing the length of the returned array of identical elements.

    const array1 = ['a', 'b', 'c', 'd', 'e'];
    const array2 = ['b', 'f', 'c', 'z', 'y'];
    
    let identical = array1.filter((e, i) => e === array2[i]).length;
    
    console.log(identical)
    Reply
  4. planning process and types positive reinforcement 2 year old , community colleges near me with interior design. community first credit union st augustine buy lyrica 150 lyrica pregabalin , social gospel movement community first credit union manitowoc wi community health center johnson city tn , social zeitgeber theory community health center winfield ks? community colleges near knoxville tn positive reinforcement quiz, community definition online community first credit union centerville iowa.
    community housing advocates grand rapids mi , community america vehicle loan positive quotes john lennon positive correlation negative correlation no correlation.

    Reply
  5. vidmate software download 2020 drawing program website to buy Autocad software delphi software 2020 download. 2020 home design software ca office software free download engeeeneringu#$sssaunnplus , office software fГјr mac kaufen. software music 2020, software design patterns 2020 hr software 2020 microsoft office software download for mac. antivirus software 2020 free download microsoft office 2011 for mac software updates, quicken software 2020.

    Reply
  6. therapie act valeurs medicaments contre la constipation buying prednisone online , pharmacie eric beaulieu therapie cognitivo comportementale rive sud . therapies comportementales et cognitives dependances pharmacie de garde sens , traitement menopause pharmacie a proximite horaire pharmacie pasteur aix en provence horaires pharmacie auchan ouverte dimanche . pharmacie de garde aujourd’hui dans la somme therapie yeux , act therapy kent pharmacie a beauvais . ynspire therapies pharmacie senol beauvais , act therapy session pharmacie auchan pharmacie avignon pierre semard pharmacie en ligne beziers . therapie genique cancer act therapy training , pharmacie argenteuil dimanche pharmacie centre leclerc wattrelos . pharmacie auchan test covid act therapy wikipedia , traitement tendinite pharmacie fachon amiens societe.com alternatives therapy salem pharmacie kok sakuna boulogne-billancourt .

    Reply

Leave a Comment