combine 2 arrays and remove duplicates javascript code example

Example 1: js delete duplicates from array

const names = ['John', 'Paul', 'George', 'Ringo', 'John'];

let unique = [...new Set(names)];
console.log(unique); // 'John', 'Paul', 'George', 'Ringo'

Example 2: javascript remove duplicate in arrays

// 1. filter()
function removeDuplicates(array) {
  return array.filter((a, b) => array.indexOf(a) === b)
};
// 2. forEach()
function removeDuplicates(array) {
  let x = {};
  array.forEach(function(i) {
    if(!x[i]) {
      x[i] = true
    }
  })
  return Object.keys(x)
};
// 3. Set
function removeDuplicates(array) {
  array.splice(0, array.length, ...(new Set(array)))
};
// 4. map()
function removeDuplicates(array) {
  let a = []
  array.map(x => 
    if(!a.includes(x) {
      a.push(x)
    })
  return a
};
/THIS SITE/ "https://dev.to/mshin1995/back-to-basics-removing-duplicates-from-an-array-55he#comments"

Example 3: javascript to remove duplicates from an array

uniqueArray = a.filter(function(item, pos) {
    return a.indexOf(item) == pos; 
})

Example 4: js merge 2 lists

var array1 = ["Vijendra", "Singh"];
var array2 = ["Singh", "Shakya"];

console.log(array1.concat(array2));
// output: ["Vijendra", "Singh", "Singh", "Shakya"]

Example 5: merge array no duiplicates js

let array1 = ['a','b','c'];
let array2 = ['c','c','d','e'];
let array3 = array1.concat(array2);
array3 = [...new Set([...array1,...array2])]; // O(n)

Example 6: javascript to remove duplicates from an array

uniqueArray = a.filter(function(item, pos) {
    return a.indexOf(item) == pos;
})