0

will be very simple question,

I have these two arrays:

var pcNumbers = [1,2,3,4,5];
var userNumbers = [1,2,7,8,9];

Pratically i have to find a way to create an alert who say "There are two elements in common, 1 and 2"

Tyia

Emanuel Rista
  • 55
  • 1
  • 6
  • Does this answer your question? [Check if an array contains any element of another array in JavaScript](https://stackoverflow.com/questions/16312528/check-if-an-array-contains-any-element-of-another-array-in-javascript) – justDan Nov 20 '20 at 13:33
  • Does this answer your question? [How can I check if an array contains a value of another array?](https://stackoverflow.com/questions/62087791/how-can-i-check-if-an-array-contains-a-value-of-another-array) – Md Sabbir Alam Nov 20 '20 at 13:39
  • 2
    Does this answer your question? [Simplest code for array intersection in javascript](https://stackoverflow.com/questions/1885557/simplest-code-for-array-intersection-in-javascript) – Aditya Menon Nov 20 '20 at 13:40
  • Here is a suitable [answer](https://stackoverflow.com/a/1885569/5481110) – Aditya Menon Nov 20 '20 at 13:41

3 Answers3

7

You can filter one array by checking each item in another using Array.prototype.filter()

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

and Array.prototype.includes()

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

var pcNumbers = [1,2,3,4,5];
var userNumbers = [1,2,7,8,9];
var commonNumbers = pcNumbers.filter(i => userNumbers.includes(i));
console.log(commonNumbers);
Mamun
  • 66,969
  • 9
  • 47
  • 59
2

Well, this is really simple question.

Go through the 1st array and check if each element appears in the 2nd array.

If so, push it to another array or just display it.

const a = [1,2,3,4,5,6,7,8,9,10];
const b = [2,4,5,7,11,15];

for(let i of a){
  if(b.includes(i)){
    console.log(i)
  }
}
--------- OR --------------
console.log(a.filter(e => b.includes(e)))
Kid
  • 1,160
  • 2
  • 14
  • 31
1

A quick solution could be something like this:

const pcNumbers = [1,2,3,8,5];
const userNumbers = [1,2,7,8,9];
const newArr = [];

for (let i = 0; i < pcNumbers.length; i++) {
    for (let j = 0; j < userNumbers.length; j++) {
        if (pcNumbers[i] === userNumbers[j]) {
            newArr.push(pcNumbers[i])
        }
    }
};

console.log(`These are in common: ${newArr.join(', ')}`)

This will result in:

These are in common: 1, 2

  • what's the expected result for `[1,2,3,8,5] and [1,2,7,8,9,2]` @Emanuel Rista – Kid Nov 20 '20 at 16:49
  • 1
    This would be an edge case. You can always check if the number is in the array before you push it. For example: if (!(pcNumbers[i] in newArr)) { newArr.push(pcNumbers[i]) } - In this case, you will do a lot of looping, and a better solution would be to add the numbers to a JS object instead (hash table). That would make the lookup a faster, but would require a bit different code. – Stephan Bakkelund Valois Nov 20 '20 at 17:24
  • So I am just saying that this is the best efficient solution. :D – Kid Nov 20 '20 at 21:03