how to remove duplicates in an array code example

Example 1: removing duplicates in array javascript

let arr = [1,2,3,1,1,1,4,5]
    
let filtered = arr.filter((item,index) => arr.indexOf(item) === index)
    
 console.log(filtered) // [1,2,3,4,5]

Example 2: how to remove duplicates in array in javascript

const numbers = [1 , 21, 21, 34 ,12 ,34 ,12];
const removeRepeatNumbers = array => [... new Set(array)]
removeRepeatNumbers(numbers) // [ 1, 21, 34, 12 ]

Example 3: remove duplicates array.filter

// removeDuplicates ES6
const uniqueArray = oldArray.filter((item, index, self) => self.indexOf(item) === index);

Example 4: remove duplicates in array

uniq = [...new Set(array)];

or 

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