How to use the map In Javascript

Union:

  • Creating functions and passing both arrays and their respective lengths.
  • Initializing a map mp and storing the element’s key value pairwise.
  • Iterating mp and storing unique value using mp.keys() function into an array.
  • Printing array in the console.

Intersection:

  • Creating functions and passing both arrays and their respective lengths.
  • Initializing a map mp and storing the elements of the first array in key-value pairwise
  • Iterating the second array and checking for the presence of each element in the mp map, If that element is present in mp then we will add that element into the intersection array.
  • And will delete that element from mp for the avoidance of duplicates in the intersection array.

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

Javascript




function printUnion(a, n, b, m) {
    let mp = new Map();
  
    // Inserting array elements in mp
    for (let i = 0; i < n; i++) {
        mp.set(a[i], i);
    }
    for (let i = 0; i < m; i++) {
        mp.set(b[i], i);
    }
  
    let ans = [];
    for (let key of mp.keys()) {
        ans.push(key)
    }
    console.log(ans);
}
function getIntersection(a, n, b, m) {
    let mp = new Map();
    let ans = [];
  
    // Inserting elements from array 'a' into the Map
    for (let i = 0; i < n; i++) {
        mp.set(a[i], i);
    }
  
    console.log("Intersection:");
    for (let i = 0; i < m; i++) {
        if (mp.has(b[i])) {
            ans.push(b[i]);
              
            // Map to avoid duplicates in the intersection
            mp.delete(b[i]);
        }
    }
    console.log(ans);
}
  
// Driver Code
  
let a = [1, 2, 5, 6, 2, 3, 5, 7, 3];
let b = [2, 4, 5, 6, 8, 9, 4, 6, 5, 4];
printUnion(a, a.length, b, b.length);
getIntersection(a, a.length, b, b.length);


Output

[
  1, 2, 5, 6,
  3, 4, 8, 9
]
Intersection:
[ 2, 5, 6 ]

Time Complexity: O(m * log(m) + n * log(n))

Auxiliary Space: 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