Use Sorting

Union:

  • The first step is to sort both arrays.
  • Now, use the two-pointer approach, if the current element is smaller then store it in ans.
  • Else, if elements are equal then simply store either the first array element or the second array.

Intersection:

  • The first step is to sort both arrays.
  • Now, use the two-pointer approach, if the current element is smaller then increment the first array otherwise increment the second array.
  • If elements are equal then simply store the element in the ans array.

Example: This example describes the union & intersection of 2 unsorted Arrays by implementing the Sorting.

Javascript




function printUnion(arr1, arr2, m, n) {
    arr1.sort(function (a, b) { return a - b; });
    arr2.sort(function (a, b) { return a - b; });
    let i = 0;
    let j = 0;
    let ans = [];
    while (i < m && j < n) {
        if (arr1[i] < arr2[j])
            ans.push(arr1[i++]);
        else if (arr2[j] < arr1[i])
            ans.push(arr2[j++]);
  
        else {
            ans.push(arr2[j++]);
            i++;
        }
    }
  
    // Printing remaining elements of the larger array
    while (i < m)
        ans.push(arr1[i++]);
    while (j < n)
        ans.push(arr2[j++]);
    console.log(ans);
}
  
// Function prints intersection of arr1 and arr2
function printIntersection(arr1, arr2, m, n) {
    arr1.sort(function (a, b) { return a - b; });
    arr2.sort(function (a, b) { return a - b; });
    let i = 0;
    let j = 0;
    let ans = [];
    while (i < m && j < n) {
        if (arr1[i] < arr2[j])
            i++;
        else if (arr2[j] < arr1[i])
            j++;
        else {
            ans.push(arr2[j++])
            i++;
        }
    }
    console.log(ans);
}
  
// Driver program to test above function
let arr1 = [7, 1, 5, 2, 3, 6];
let arr2 = [3, 8, 6, 20, 7];
let m = arr1.length;
let n = arr2.length;
  
console.log("Union of two arrays is : ");
printUnion(arr1, arr2, m, n);
  
console.log("Intersection of two arrays is : ");
printIntersection(arr1, arr2, m, n);


Output

Union of two arrays is : 
[
  1, 2, 3,  5,
  6, 7, 8, 20
]
Intersection of two arrays is : 
[ 3, 6, 7 ]

Time complexity: O(mLogm + nLogn)

Space Complexity: O(m + n)

JavaScript Program to Find Union and Intersection of Two Unsorted Arrays

In this article, we will learn how to find the Union and Intersection of two arrays. When an array contains all the elements that are present in both of the arrays, it is called a union. On the other hand, if an array has only those elements that are common in both of the arrays, then it is called an intersection.

Example

Input: 
a = [1,3,5,7,9] , b = [1,2,3,4,6,8,10]

Output:
Union: [1,2,3,4,5,6,7,8,9,10]
Intersection: ans =[1,3]

Table of Content

  • Using Set
  • Using the map
  • Use Sorting
  • Use Sorting and Searching
  • Without using hashing or any predefined library like sets or maps and works even for both repeated and distant elements
  • Kind of hashing technique without using any predefined JavaScript Collections

We will understand the various approaches for finding the union and the intersection of two unsorted Arrays.

Similar Reads

Using Set

Union:...

Using the map

...

Use Sorting

Union:...

Use Sorting and Searching

...

Without using hashing or any predefined library like sets or maps and works even for both repeated and distant elements

Union:...

Kind of hashing technique without using any predefined JavaScript Collections

...

Contact Us