How to find every element that exists in any of two given arrays once using JavaScript ?
Last Updated :
27 May, 2024
In this article, we will learn how to find every element that exists in any of the given two arrays. To find every element that exists in any of two given arrays, you can merge the arrays and remove any duplicate elements.
Method 1: Using Set
A set is a collection of unique items i.e. no element can be repeated. We will add all elements of two arrays to the set, and then we will return the set.
Example: In this example, we will see the use of the Javascript set method to find every element that exists in any of the two given arrays once.
JavaScript
// Function which takes an array as argument
const print = (arr1,arr2) => {
// Creating a set with elements of arr1
const set = new Set(arr1)
// Adding elements of arr2
arr2.forEach(element => {
set.add(element)
});
// Returning resultant array
return set
}
// Input array
const arr1 = [10, 20, 30, 40, 50]
const arr2 = [10,20,34,32,11]
// Printing the result
console.log(print(arr1,arr2))
OutputSet(8) { 10, 20, 30, 40, 50, 34, 32, 11 }
Method 2: Using loop
In this approach, we will choose one array and then we will run a loop on the second array and check whether an element of this array is present in the first array or not. If an element is already present, we skip otherwise we will add this to the first array.
Example: In this example, we will see the use of Javascript loops to find every element that exists in any of the two given arrays once.
JavaScript
// Javascript program to find all element
// present in any of two given arrays
// Function which takes an array as argument
const print = (arr, arr2) => {
// A counter for adding element
let k = arr.length
// Checking every element and
// adding required element
arr2.forEach(element => {
if (arr.indexOf(element) == -1) {
arr[k] = element
k++
}
});
// Returning resultant array
return arr
}
// Input array
const arr1 = [1, 2, 3, 4, 5]
const arr2 = [1, 2, 3, 4]
// Printing the result
console.log(print(arr1, arr2))
In this method, we will use the concat() method to merge the array and filter() method for removing the element which repeats.
Example:
JavaScript
function findElementsInArr(arr1, arr2) {
let mergedArray = arr1.concat(arr2);
let uniqueEle = mergedArray.filter(function (element, index, self) {
return self.indexOf(element) === index;
});
return uniqueEle;
}
let arr1 = [1, 2, 3, 4];
let arr2 = [3, 4, 5, 6];
let result = findElementsInArr(arr1, arr2);
console.log(result);
Output[ 1, 2, 3, 4, 5, 6 ]
Method 5: Using reduce and includes
Using reduce and includes, combine two arrays and build a new array with unique elements. The reduce method iterates through the combined array, adding each element to the accumulator if it doesn't already include it, ensuring no duplicates.
Example: In this example we combines array1 and array2 using concat, then uses reduce to create a union array containing unique elements by checking for duplicates with includes.
JavaScript
const array1 = [1, 2, 3];
const array2 = [3, 4, 5];
const combinedArray = array1.concat(array2);
const union = combinedArray.reduce((acc, item) => {
if (!acc.includes(item)) {
acc.push(item);
}
return acc;
}, []);
console.log(union);
Similar Reads
How to get the elements of one array which are not present in another array using JavaScript? The task is to get the elements of one array that are not present in another array. Here we are going to use JavaScript to achieve the goal. Here are a few techniques discussed. Approach Take the arrays in variables.Use the .filter() method on the first array and check if the elements of the first a
2 min read
How to Create an Array using Intersection of two Arrays in JavaScript ? In this article, we will try to understand how to create an array using the intersection values of two arrays in JavaScript using some coding examples. For Example: arr1 = [1, 3, 5, 7, 9, 10, 14, 15]arr2 = [1, 2, 3, 7, 10, 11, 13, 14]result_arr = [1, 3, 7, 10, 14]Approach 1: We will see the native a
3 min read
Find Common Elements in all Rows of a Given Matrix using JavaScript Matrix manipulation takes place in a large number of applications, and the detection of shared elements among rows is one of the common challenges. Example: Input : [1, 2, 3], [2, 3, 4], [3, 4, 5]Output: [3]Input : [1, 2, 3], [4, 5, 6], [7, 8, 9]Output: [ ]There are the following approaches for find
3 min read
Find Common Elements In Three Sorted Arrays using JavaScript JavaScript can be used to find the common elements in given three sorted arrays. We are given three different sorted arrays, we have to return common elements of three arrays. Example:Input array1 = [1 , 2 , 3 , 4 ,5 ] array2 = [3 , 4, 5 , 6 ,7 ]array3 = [ 3 , 4 , 7 , 8 , 9] Output [3 , 4]Below are
4 min read
How to Find the Index of an Element that Contains the Given Substring in Array ? To find the index of an element within an array that contains a specific substring in JavaScript, there are multiple strategies available. The methods offer flexibility in handling various scenarios, allowing you to efficiently locate the index of the desired substring within the array. Table of Con
2 min read
How to get symmetric difference between two arrays in JavaScript ? In this article, we will see how to get the symmetric difference between two arrays using JavaScript. In Mathematics the symmetric difference between two sets A and B is represented as A ΠB = (A - B) ⪠(B - A) It is defined as a set of all elements that are present in either set A or set B but not
6 min read
JavaScript Program to Find Element that Appears once in Array where Other Elements Appears Twice Given an Array of Integers consisting of n elements where each element appears twice except one element. The task is to find the element which appears only once in the input array in JavaScript. Example: Input : arr = [ 5, 9, 2, 7, 4, 8, 6, 1, 5, 1, 4, 6, 3, 7, 8, 2, 9 ] Output : 3Explanation: The i
3 min read
Find Kth Element of Two Sorted Arrays in JavaScript Given two sorted arrays, our task is to find the Kth element in the combined array made by merging the two input arrays in JavaScript.Example:Input: Arr1: [1, 2, 5] , Arr2:[2, 4, 6, 8], K = 4Output: Kth element is 4.Explanation:The final Array would be: [1, 2, 2, 4, 5, 6, 8]The 4th element of this a
3 min read
Find the Common Elements of More than Two JavaScript Arrays? Given an HTML document having multiple arrays with some elements and the task is to get the common elements from arrays with the help of JavaScript. Below are the approaches to find the common elements of more than two JavaScript Arrays:Table of ContentUsing filter methodUsing reduce method Approach
3 min read
How to Check if an Element Exists in an Array in JavaScript? Given an array, the task is to check whether an element present in an array or not in JavaScript. If the element present in array, then it returns true, otherwise returns false.The indexOf() method returns the index of first occurance of element in an array, and -1 of the element not found in array.
2 min read