In JavaScripts there are multiple ways to remove duplicates element in array here we discuses several ways to remove duplicates element in array
1. Remove duplicate elements an array using ...new Set() Method
function RemoveDuplicatesValue(values){
var UniqueValue;
return UniqueValue=[...new Set(values)];
}
var values = ["A", "B", "B", "C", "D", "C", "C", "E", "F", "G"];
var uniqueValues=RemoveDuplicatesValue(values);
console.log(uniqueValues)
//Output: ['A', 'B', 'C', 'D', 'E', 'F', 'G']
2. Remove duplicate elements Using indexOf() and Push() Method
function RemoveDuplicatesValue(values){
var uniqueNames = [];
//Start For Loop
for(i=0; i < values.length; i++){
if(uniqueNames.indexOf(values[i]) === -1) {
uniqueNames.push(values[i]);
}
}
return uniqueNames;
}
var values = ["A", "B", "B", "C", "D", "C", "C", "E", "F", "G"];
var uniqueNames = RemoveDuplicatesValue(values);
console.log(uniqueNames);
// Output: ['A', 'B', 'C', 'D', 'E', 'F', 'G']