Javascript program to merge two arrays and remove duplicate items

Using the spread operator and Set() function.

const a = [4,3,2,1,7,8]
const b = [4,10,12,1,9]

let arr = [...a,...b]
console.log("The array with duplicates\n"+arr)

const new_arr = [...new Set(arr)]
console.log("The array without duplicates\n"+new_arr)

The output of the code is

The array with duplicates
4,3,2,1,7,8,4,10,12,1,9"

The array without duplicates
4,3,2,1,7,8,10,12,9"

  • We will create two arrays a and b
  • Using the spread operator we will merge the two arrays and store them in a thrid array.
  • The new array will have duplicates.
  • Using the Set() function we will remove the duplicates of the array.
Facebook
Twitter
LinkedIn
Pinterest

Leave a Reply

Your email address will not be published. Required fields are marked *

ABOUT ME !!
Johan William

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

RECENT POSTS

TEST