Javascript Algorithm Array Common Element among arrays

Description

Javascript Algorithm Array Common Element among arrays

function commonElements(kArray) {
    var hashmap = {},
        last, answer = [];// w w w . j  a  v  a 2  s .  co m

    for (var i = 0, kArrayLength = kArray.length; i < kArrayLength; i++) {
        var currentArray = kArray[i];
        last = null;
        for (var j = 0, currentArrayLen = currentArray.length; j < currentArrayLen; j++) {
            var currentElement = currentArray[j];
            if (last != currentElement) {
                if (!hashmap[currentElement]) {
                    hashmap[currentElement] = 1;
                } else {
                    hashmap[currentElement]++;
                }
            }
            last = currentElement;
        }
    }

    // Iterate through hashmap
    for (var prop in hashmap) {
        if (hashmap[prop] == kArray.length) {
            answer.push(parseInt(prop));
        }
    }
    return answer;
}

let a = commonElements([
    [1, 2, 3],
    [1, 2, 3, 4],
    [1, 2]
]);

console.log(a);



PreviousNext

Related