JSFiddle - React, Tailwind, and code Playground
by DineshAngappa
JavaScript
var a = [1,2,3,3,2,5,3,8,10,11,10];
//Brute-force approach
function firstMethod(a){
let len = a.length;
let _temp = [];
for(let i=0; i< len; i++){
if(_temp.indexOf(a[i]) == -1){
_temp.push(a[i]);
}
}
return _temp;
}
console.log(firstMethod(a));
// Quick Sort approach
function secondMethod(b){
let len = b.length;
let _temp = [];
b.sort();
//console.log(b);
for(let i=0; i<len;i++)
{
if(b[i] !==b[i-1]){
_temp.push(b[i]);
}
}
return _temp;
}
console.log(secondMethod(a));
//Object keys
function thirdMethod(c){
let len = c.length;
let _temp = [];
for(let i in c){
_temp[c[i]] = true
}
//console.log(_temp);
return Object.keys(_temp);
}
console.log(thirdMethod(a));