How to find every element that exists in any of two given arrays once using JavaScript?


In this tutorial, we are going to learn how can we find every element that exists in any of the two given arrays once means we need to take two arrays filled with some elements and then from those two arrays create a new array which contains all the elements present in both the array once i.e., no element should be repeated in the new array created.

For example, we take two arrays and find every element that exists in any of the two arrays once

Input

const arr1 = [1, 2, 3, 4, 5]
const arr2 = [1, 4, 9, 16, 25]

Output

result = [1, 2, 3, 4, 5, 9, 16, 25]

Input

const arr1 = [ ‘Deepansh’, ’Aditya’, ’Rishabh’, ’Rishit’ ]
const arr2 = [ ‘Vikrant’, ’Rishabh’, ’Mohit’, ’Rishit’ ]

Output

result = [ ‘Deepansh’, ’Aditya’, ’Rishabh’, ’Rishit’, ’Vikrant’, ’Mohit’]

There are basically two approaches using which we can find every element that exists in any of two given arrays once using JavaScript which are given as −

Approach 1 − By Looping through Arrays

Here we are going to choose one of the two arrays and loop through the other arrays by following the steps given below −

Step 1 − In the very first step we create two different arrays which some the elements in both the arrays same so that we can check our result clearly.

Step 2 − Now we create a function taking the two arrays as argument in it.

Step 3 − Find out the length of any of the array.

Step 4 − In this step we will loop through the other array using array.forEach() method.

Step 5 − Now we check every individual element is present or not in the first array or not if it is not present there than we add it in the first array also updating the length of first array.

Step 6 − Now we call the function check to get the desired result.

Example

We can use the below code to find every element that exists in any of two given arrays once by looping approach −

<!DOCTYPE html> <html> <head> <h2> Tutorials Point </h2> </head> <body> <script> const arr1 = [ 1, 2, 3, 4, 5 ] const arr2 = [ 1, 4, 9, 16, 25 ] const check = (arr1, arr2) => { let len = arr1.length arr2.forEach(value => { if (arr1.indexOf(value) == -1) { arr1[len] = value len++ } }); return arr1 } let result = check(arr1, arr2); document.write(result) </script> </body> </html>

Approach 2 − By Creating a Set in JavaScript

In this approach we are going to create a set (a collection of unique elements) and add the elements of both arrays into the set so that we get the array of unique elements. The steps to produce the set are given below −

Step 1 − In the very first step we create two different arrays which some the elements in both the arrays same so that we can check our result clearly.

Step 2 − Now we create a function taking the two arrays as argument in it.

Step 3 − In this step we are going to create a set with the elements of first array.

Step 4 − In this step we will loop through the other array using array.forEach() method.

Step 5 − Now we add the elements of array 2 into the set and the set will accept only that elements which are not present in it.

Step 6 − Now we call the function check to get the desired result.

Example

We can use the below code to find every element that exists in any of two given arrays once by creating set approach −

<!DOCTYPE html> <html> <head> <h2> Tutorials Point </h2> </head> <body> <div id="demo"> </div> <script> const arr1 = [ 1, 2, 3, 4, 5 ] const arr2 = [ 1, 4, 9, 16, 25 ] const check = (arr1,arr2) => { const arr_set = new Set(arr1) arr2.forEach(element => { arr_set.add(element) }); for (let item of arr_set.values()) document.getElementById('demo').innerHTML += item +", "; } check(arr1,arr2) </script> </body> </html>

In this tutorial, we discussed two approaches to finding every element that exists in any of the two given arrays once using JavaScript. The first method is "By Looping through Arrays" and the second approach is "By Creating a Set in JavaScript".

Updated on: 18-Oct-2022

159 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements