JSFiddle - React, Tailwind, and code Playground

by pseudosavant

JavaScript

var names1 = ["john", "steve", "joe", "tom", "marco", "eric", "buddy"];
var names2 = ["joe", "marco", "buddy", "chris", "tim", "clarke", "pat"];

var intersection = function(firstArray, secondArray) {
    var matches = {}, results = [], a = firstArray, b = secondArray, i, l;
    
    for (i=0, l=a.length; i<l; i++) {
        matches[a[i]] = 1;
    }
    
    for (i=0, l=b.length; i<l; i++) {
        if (matches[b[i]]) matches[b[i]]++;
    }
    
    for (i in matches) {
        if (matches[i] === 2) results.push(i);
    }
    
    return results;
};

console.log(intersection(names1,names2));