How to print unique elements from two unsorted arrays using JavaScript ?

Given two unsorted arrays, the task is to write a JavaScript program to print the unique (uncommon) elements in the two arrays.

These are the way by which we print unique elements from two unsorted arrays using JavaScript:

Table of Content

  • Using for loop
  • Using filter() method
  • Using Sets

Using for loop

  • Create an empty array that would store the unique elements from the two arrays.
  • Iterate over all elements of array1 using a loop.
  • Set the initial flag value as 0 for each iteration.
  • In that loop Iterate over all elements of array2 using another loop and check if array1[element] is present in array2.
  • If present, remove the element from array2 using the splice() method and set the flag to 1.
  • If array2 has been fully traversed for array1[element] and the flag is still equal to 0, add the array1[element] to the unique elements array.
  • Repeat steps 4-6 for each element in array 1.
  • Finally, push array2 to the unique elements array since all duplicate elements have been removed from array2 (refer to Step 5).

Example: The implementation of the above approach is given below.

Javascript
function unique(arr1, arr2, uniqueArr) {
    for (let i = 0; i < arr1.length; i++) {
        let flag = 0;
        for (let j = 0; j < arr2.length; j++) {
            if (arr1[i] === arr2[j]) {
                arr2.splice(j, 1);
                j--;
                flag = 1;
            }
        }

        if (flag == 0) {
            uniqueArr.push(arr1[i]);
        }
    }
    uniqueArr.push(arr2);
    return uniqueArr;
}

let arr1 = [54, 71, 58, 95, 20];
let arr2 = [71, 51, 54, 33, 80];

let uniqueArr = [];

console.log("Unique elements in the two arrays are: "
    + unique(arr1, arr2, uniqueArr).flat());

Output
Unique elements in the two arrays are: 58,95,20,51,33,80

Using filter() method

We can filter all the element which is unique by using the filter() method. Then we will make one new array in which we concat our filtered array.

Example:

Javascript
let arr1 = [54, 71, 58, 95, 20];
let arr2 = [71, 51, 54, 33, 80];

let unique1 = arr1.filter((o) => 
              arr2.indexOf(o) === -1);
let unique2 = arr2.filter((o) => 
              arr1.indexOf(o) === -1);

const unique = unique1.concat(unique2);
console.log(unique);

Output
[ 58, 95, 20, 51, 33, 80 ]

Using Sets

To 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. This ensures that duplicate elements are removed automatically, providing the unique elements.

Example: In this example we creates sets from arr1 and arr2, filters out items unique to each set, and combines them into a single array unique. It logs the array containing unique elements from both arrays.

JavaScript
const arr1 = [54, 71, 58, 95, 20];
const arr2 = [71, 51, 54, 33, 80];

const set1 = new Set(arr1);
const set2 = new Set(arr2);

const unique1 = arr1.filter(item => !set2.has(item));
const unique2 = arr2.filter(item => !set1.has(item));

const unique = [...unique1, ...unique2];
console.log(unique); 

Output
[ 58, 95, 20, 51, 33, 80 ]