Following are the 2 ways to find distinct or unique elements from an array
- Using ES6 new Set
var array = [3,7,5,3,2,5,2,7];
var unique_array = [...new Set(array)];
console.log(unique_array); // output = [3,7,5,2]
- Using For Loop
var array = [3,7,5,3,2,5,2,7];
for(var i=0;i<array.length;i++)
{
for(var j=i+1;j<array.length;j++)
{
if(array[i]===array[j])
{
array.splice(j,1);
}
}
}
console.log(array); // output = [3,7,5,2]
Top comments (0)