Intersection and Union of Array in JavaScript

What is union of Arrays?

Union of arrays would represent a new array combining all elements of the input arrays, without repetition of elements.

let arrOne = [10,15,22,80];
let arrTwo = [5,10,11,22,70,90];

// Union of Arrays
let arrUni…


This content originally appeared on DEV Community and was authored by RajeshKumarYadav.com

Array and Union

What is union of Arrays?

Union of arrays would represent a new array combining all elements of the input arrays, without repetition of elements.

let arrOne = [10,15,22,80];
let arrTwo = [5,10,11,22,70,90];

// Union of Arrays
let arrUnion = [...new Set([...arrOne, ...arrTwo])];
console.log(arrUnion);

What is intersection of Arrays?

The intersection of two arrays is a list of distinct numbers which are present in both the arrays. The numbers in the intersection can be in any order.

let arrOne = [10,15,22,80];
let arrTwo = [5,10,11,22,70,90];

// Intersection of Arrays
let arrIntersection = arrOne.filter((v) =>{
    return arrTwo.includes(v);
});
console.log(arrIntersection);

Demo -


This content originally appeared on DEV Community and was authored by RajeshKumarYadav.com


Print Share Comment Cite Upload Translate Updates
APA

RajeshKumarYadav.com | Sciencx (2021-09-20T07:01:51+00:00) Intersection and Union of Array in JavaScript. Retrieved from https://www.scien.cx/2021/09/20/intersection-and-union-of-array-in-javascript/

MLA
" » Intersection and Union of Array in JavaScript." RajeshKumarYadav.com | Sciencx - Monday September 20, 2021, https://www.scien.cx/2021/09/20/intersection-and-union-of-array-in-javascript/
HARVARD
RajeshKumarYadav.com | Sciencx Monday September 20, 2021 » Intersection and Union of Array in JavaScript., viewed ,<https://www.scien.cx/2021/09/20/intersection-and-union-of-array-in-javascript/>
VANCOUVER
RajeshKumarYadav.com | Sciencx - » Intersection and Union of Array in JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/20/intersection-and-union-of-array-in-javascript/
CHICAGO
" » Intersection and Union of Array in JavaScript." RajeshKumarYadav.com | Sciencx - Accessed . https://www.scien.cx/2021/09/20/intersection-and-union-of-array-in-javascript/
IEEE
" » Intersection and Union of Array in JavaScript." RajeshKumarYadav.com | Sciencx [Online]. Available: https://www.scien.cx/2021/09/20/intersection-and-union-of-array-in-javascript/. [Accessed: ]
rf:citation
» Intersection and Union of Array in JavaScript | RajeshKumarYadav.com | Sciencx | https://www.scien.cx/2021/09/20/intersection-and-union-of-array-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.