Remove duplicate from array

by Krishna Ananthi

JavaScript

let a = [1, 2, 5, 3, 2, 1];
let b = [];
// brute force method
let len = a.length;
for (let i = 0; i < len; i++) {
  if (b.indexOf(a[i]) == -1)
    b.push(a[i]);
}
console.log(b)
//sort and remove method
a = [1, 2, 5, 3, 2, 1];
b = [];
a.sort();
let temp;
for (let i = 0; i < len; i++) {
  if (a[i] != temp) {
    b.push(a[i]);
    temp = a[i];
  }
}
console.log(b);
// using obj
a = [1, 2, 5, 3, 2, 1];
b = [];
let obj = {};
for (let i of a) {
  obj[i] = true;
}
b = Object.keys(obj).map(i=>parseInt(i))
console.log(b);
// using set
a = [1, 2, 5, 3, 2, 1];
console.log([... new Set(a)])