remove same element array javascript code example
Example 1: 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)
Example 2: javascript remove duplicate strings from array
let uniqueArray = [...new Set(arrayWithDuplicates)];
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;
}