In this article, we will explore how to use JavaScript to perform an intersection between two arrays.

What is Intersection of Two Arrays?

The intersection of two arrays is a set of values in both arrays. This operation returns a new array that contains only the common elements from both arrays, with no duplicates. The result of the intersection operation is a new array with the same data type as the original arrays.

Why Perform Intersection Between Two Arrays?

The intersection between two arrays is helpful in many cases, such as when we must compare two arrays to find the common elements. For example, we may need to find common friends between two users in a web application. In this case, we can perform the intersection between the two arrays of friends to find common friends.

Another use case is when we need to find the common elements between two data arrays, such as finding the common elements between two lists of numbers. This operation can help us filter out the elements that are not common between the two arrays and keep only the common elements.

JavaScript Program to Perform Intersection Between Two Arrays

There are several ways to perform the intersection between two arrays in JavaScript. Still, in this article, we will focus on three methods: using the filter() method, reduct(), and using the set() object.

Method 1: Using the filter()

The filter method is one of JavaScript’s most straightforward methods for intersecting two arrays. It works by looping through each element in the first array and checking if it is present in the second array. If the part is found in the second array, it is added to a new array, which is then returned as the result of the intersection.

				
					function intersect(a, b) {
  return a.filter(function(value) {
    return b.indexOf(value) !== -1;
  });
}

				
			

This method is easy to understand and can quickly intersect two arrays in JavaScript. However, there are more efficient methods than this one, as it requires looping through each element in both arrays and checking their presence in the other array.

Method 2: Using the Reduct()

The reduce method is more efficient for intersecting two arrays in JavaScript. It works by reducing the arrays to a single value and using the concert method to combine the two arrays.

				
					function intersect(a, b) {
  return a.reduce(function(result, value) {
    if (b.indexOf(value) !== -1) {
      result.push(value);
    }
    return result;
  }, []);
}

				
			

This method is faster than the filter method, as it only requires looping through each element in one array and checking its presence in the other array. Additionally, it allows developers to easily add additional features to the result array, making it a flexible and powerful tool for intersecting two arrays in JavaScript.

Method 3: Using the Set()

The Set method is another effective method for intersecting two arrays in JavaScript. This method works by converting each array into a set and using the intersection method to find the elements common to both arrays.

				
					function intersect(a, b) {
  let setA = new Set(a);
  let setB = new Set(b);
  let intersection = new Set([...setA].filter(x => setB.has(x)));
  return Array.from(intersection);
}

				
			

The Set method is the most efficient method for intersecting two arrays in JavaScript, as it only requires looping through each element in one array and checking its presence in the other set. Additionally, it allows developers to quickly find unique features in the arrays, making it a valuable tool for data processing and management.


Thanks for reading. Happy coding!