Languages
[Edit]
EN

JavaScript - merge two arrays and de-duplicate items

0 points
Created by:
Mariam-Barron
811

In this article, we would like to show you how to merge two arrays and remove duplicate items using JavaScript.

Quick solution:

 

Practical examples

Edit

1. Set based approach

Edit

In this example, we use Set based approach to prevent item duplication while adding them to the result array.

2. Simple types approach

Edit

In this example, we use simple types to merge arrays without duplicates.

Note:

This solution should be used when each item has the same type in the array, and the type is one of: boolean, number, or string.

 

See also

Edit
  1. JavaScript - merge two arrays

  2. JavaScript - remove duplicates from array

References

Edit
  1. Array.prototype.filter() - JavaScript | MDN
  2. Set() constructor - JavaScript | MDN
  3. Object.prototype.hasOwnProperty() - JavaScript | MDN

Alternative titles

  1. JavaScript - merge two arrays and remove duplicates
  2. JavaScript - merge two arrays and remove duplicate items
  3. JavaScript - getting union of two arrays
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join