javascript remove duplicate numbers from array 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 remove duplicate strings from array

//ES6
let uniqueArray = [...new Set(arrayWithDuplicates)];

//Alternative
function removeArrayDuplicates(arrayWithDuplicates) {
    let seen = {};
    let uniqueArray = [];
    let len = arrayWithDuplicates.length;
    let j = 0;
    for(let i = 0; i < len; i++) {
         let item = arrayWithDuplicates[i];
         if(seen[item] !== 1) {
               seen[item] = 1;
               uniqueArray[j++] = item;
         }
    }
    return uniqueArray;
}

Example 4: remove duplicates from array javascript

arr.filter((v,i,a)=>a.findIndex(t=>(t.place === v.place && t.name===v.name))===i)