Check If Arrays Contain Common Elements In JavaScript

Don’t worry if you need to check if arrays contain common elements during JavaScript application development. This article will share some solutions to check if arrays contain common elements in JavaScript using the for loop or the some() and includes() methods. Let’s go.

How to check if arrays contain common elements in JavaScript

We will use the following two solutions to check if arrays contain common elements:

  • Using the for loop
  • Using the some() and includes() methods

Now, we will go to each solution with specific examples.

Using the for loop

This solution uses two for loops. The first loop iterates through all the elements of the first array. The second loop iterates through all the elements of the second array.

At each loop, we compare each element of the first array with each element of the second.

The program will return true if two arrays have at least one element in common. The program will return false if the two arrays do not have elements in common.

Example:

// Check if two arrays contain common elements
function checkCommon(arr1, arr2) {
    // Loop through all elements of the 'arr1' array
    for (let i = 0; i < arr1.length; i++) {

        // Loop through all elements of the 'arr2' array
        for (let j = 0; j < arr2.length; j++) {
            if (arr1[i] === arr2[j]) {
                return true;
            }
        }
    }
    return false;
}

// Create arrays
const array1 = ['Learn', 'Share', 'IT'];
const array2 = ['Java', 'JavaScript', 'Python'];
const array3 = ['R', 'C#', 'JavaScript'];

// Check if the 'array1' array and the 'array2' array contain common elements
console.log(checkCommon(array1, array2));

// Check if the 'array2' array and the 'array3' array contain common elements
console.log(checkCommon(array2, array3));

Output:

false
true

Using the some() and includes() methods

In another article, we have described the syntax and usage of the some() and includes() methods. Click here to learn more.

We perform the following steps to check if two arrays contain common elements using the some() and includes() methods.

First, we use the some() method with the first array to check each element of this array.

At each loop, we use the includes() method with the second array to check if the element is checked (in the first array) exists in the second array.

If any element in the first array exists in the second array, the some() method will return true.

Example:

// Check if two arrays contain common elements
function checkCommon(array1, array2) {
    return array1.some((element) => array2.includes(element))
}

// Create arrays
const array1 = ['Learn', 'Share', 'IT'];
const array2 = ['Java', 'JavaScript', 'Python'];
const array3 = ['R', 'C#', 'JavaScript'];

// Check if the 'array1' array and the 'array2' array contain common elements
console.log(checkCommon(array1, array2));

// Check if the 'array2' array and the 'array3' array contain common elements
console.log(checkCommon(array2, array3));

Output:

false
true

Summary

We have shared two solutions to check if arrays contain common elements in JavaScript. We recommend using the second solution for your program to make your code look cleaner. Thank you for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *