JavaScript - Print Unique Elements From Two Unsorted Arrays Last Updated : 07 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Here are the different approaches to print unique elements from two unsorted arrays using JavaScript1. Using filter() methodWe can filter all the unique elements using the filter() method. Then we will make one new array to concat our filtered array. JavaScript let a1 = [54, 71, 58, 95, 20]; let a2 = [71, 51, 54, 33, 80]; let uni1 = a1.filter((o) => a2.indexOf(o) === -1); let uni2 = a2.filter((o) => a1.indexOf(o) === -1); const res = uni1.concat(uni2); console.log(res); Output[ 58, 95, 20, 51, 33, 80 ] In this example,uni1 filters elements in a1 that are not in a2.uni2 filters elements in a2 that are not in a1.concat combines the unique elements from both arrays.Unique elements from a1 and a2.2. Using SetsTo print unique elements from two unsorted arrays in JavaScript using Sets, combine the arrays into a Set, then convert the Set back into an array. JavaScript const a1 = [54, 71, 58, 95, 20]; const a2 = [71, 51, 54, 33, 80]; const set1 = new Set(a1); const set2 = new Set(a2); const uni1 = a1.filter(item => !set2.has(item)); const uni2 = a2.filter(item => !set1.has(item)); const res = [...uni1, ...uni2]; console.log(res); Output[ 58, 95, 20, 51, 33, 80 ] In this exampleset1 and set2 store unique values of a1 and a2.uni1 filters elements in a1 not in set2.uni2 filters elements in a2 not in set1.Combine uni1 and uni2 using the spread operator for unique elements.Unique elements from both arrays.3. Using a Frequency MapA frequency map provides a structured way to count occurrences across both arrays. JavaScript const a1 = [54, 71, 58, 95, 20]; const a2 = [71, 51, 54, 33, 80]; const a = [...a1, ...a2]; const freq = a.reduce((acc, el) => { acc[el] = (acc[el] || 0) + 1; return acc; }, {}); const elem = Object.keys(freq).filter(key => freq[key] === 1).map(Number); console.log(elem); Output[ 20, 33, 51, 58, 80, 95 ] In this examplecombine a1 and a2 into a.Use reduce to create a frequency count of each element.Filter keys with a frequency of 1 (unique elements) and convert them back to numbers.4. Using External Libraries (Lodash)Libraries like Lodash simplify tasks like finding unique elements. JavaScript const _ = require('lodash'); const uniqueElements = _.xor(arr1, arr2); console.log(uniqueElements); Comment More infoAdvertise with us Next Article JavaScript - Print Unique Elements From Two Unsorted Arrays S SAKSHIKULSHRESHTHA Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA JavaScript-Questions +1 More Similar Reads Javascript Program to Print uncommon elements from two sorted arrays Given two sorted arrays of distinct elements, we need to print those elements from both arrays that are not common. The output should be printed in sorted order. Examples :Input : arr1[] = {10, 20, 30} arr2[] = {20, 25, 30, 40, 50}Output : 10 25 40 50We do not print 20 and 30 as theseelements are pr 2 min read JavaScript Program to Print All Distinct Elements in an Integer Array Given an Array of Integers consisting of n elements with repeated elements, the task is to print all the distinct elements of the array using JavaScript. We can get the distinct elements by creating a set from the integer array. Examples: Input : arr = [ 1, 2, 3, 4, 4, 5, 5, 6, 7, 7, 8, 9, 9 ] Outpu 3 min read How to find every element that exists in any of two given arrays once using JavaScript ? 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. Table of Content Using SetUsing loopUsing filter() and concat()Using reduce a 3 min read JavaScript - How To Get Distinct Values From an Array of Objects? Here are the different ways to get distinct values from an array of objects in JavaScript1. Using map() and filter() MethodsThis approach is simple and effective for filtering distinct values from an array of objects. You can use map() to extract the property you want to check for uniqueness, and th 4 min read How to remove duplicate values from array using PHP? In this article, we will discuss removing duplicate elements from an array in PHP. We can get the unique elements by using array_unique() function. This function will remove the duplicate values from the array.Syntax:array array_unique($array, $sort_flags);Note: The keys of the array are preserved i 4 min read Like