2

function commonElement(array1, array2) {
  var count = 0;
  for (var i = 0; i < array1.length; i++) {
    for (var j = 0; j < array2.length; j++) {
      if (array1[i] === array2[j]) {
        count++;
      }
    }
  }
  return count
}

console.log(commonElement([5, 2, 8, 9, 4, 7], [3, 2, 9, 5, 7]))

*when I Put non-unique value in my array then output is *

console.log(commonElement([5,2,2,8,9,4,7],[3,2,9,5,7])) //output is 5

But i want to my ouput is 4 because 2,2 compare to 2 its only 2 count output is 5

Ori Drori
  • 183,571
  • 29
  • 224
  • 209
Arvind kumar
  • 87
  • 2
  • 8

4 Answers4

0

Clone one of the arrays first (to avoid mutating), then iterate over the other array, using findIndex to find a matching element in the cloned array. If it exists, splice it out and add 1 to count:

function commonElement(array1, array2) {
  const arr1 = array1.slice();
  let count = 0;
  for (const val of array2) {
    const index = arr1.indexOf(val);
    if (index !== -1) {
      count++;
      arr1.splice(index, 1);
    }
  }
  return count;
}
console.log(commonElement([5, 2, 2, 8, 9, 4, 7], [3, 2, 9, 5, 7]))
console.log(commonElement([2, 2], [2, 2]))

To decrease computational complexity from O(n^2) to O(n), count up one of the arrays into an object first instead:

function commonElement(array1, array2) {
  const arr1Counts = {};
  for (const val of array1) {
    arr1Counts[val] = (arr1Counts[val] || 0) + 1;
  }
  let count = 0;
  for (const val of array2) {
    if (arr1Counts[val]) {
      arr1Counts[val]--;
      count++;
    }
  }
  return count;
}
console.log(commonElement([5, 2, 2, 8, 9, 4, 7], [3, 2, 9, 5, 7]))
console.log(commonElement([2, 2], [2, 2]))
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

Just do with few line of code

var array1 =[5, 2, 2, 8, 9, 4, 7];
var array2 =[3, 2, 9, 5, 7];
var common = array1.filter(function(obj) { return array2.indexOf(obj) == -1; });
console.log(common);
Sourav Singh
  • 878
  • 1
  • 8
  • 14
0

You could count up same items form the first array and down from the second array and check id the count is greater or equal to zero, the count one for the result.

function commonElement(array1, array2) {
    var counts = {},
        count = 0;

    array1.forEach(v => counts[v] = (counts[v] || 0) + 1);
    array2.forEach(v => (counts[v] = (counts[v] || 0) - 1) >= 0 && ++count);

    return count;
}

console.log(commonElement([5, 2, 8, 9, 4, 7], [3, 2, 9, 5, 7]));
console.log(commonElement([5, 2, 2, 8, 9, 4, 7], [3, 2, 9, 5, 7]));
console.log(commonElement([2, 2], [2, 2]));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
-1

The simplest way to get the number of elements which are common in both arrays will be:

// initialise two arrays

var listA = [1,2,2,3,4,5,6,7];
var listB = [2,4,6,8];


function checkCommonElements(array1, array2) {

    // get common values from array

    var filterValues = array1.filter(value => -1 !== array2.indexOf(value))

    // map values from without repeating values

    const arr = [];
    filterValues.map((x) => {
        if (arr.indexOf(x) === -1) {
            arr.push(x);
        }
    })

    // return the length of array

    return arr.length;
}

var result = checkCommonElements(listA, listB);
console.log(result);

// output is 3.

Gives you the no of common elements among two arrays without repeating any item.