Remove duplicates from array

by Shridhar Baddur

JavaScript

let a = [1, 2, 3, 5, 1, 2, 8];
let b = [];
let len = a.length;

//solution 1
/* for (let i = 0; i < len; i++) {
  if (b.indexOf(a[i]) === -1) {
    b.push(a[i]);
  }
}
 */

//solution 2
a.sort();
let temp;
for (let i = 0; i < len; i++) {
  if (a[i] !== temp) {
    b.push(a[i]);
    temp = a[i];
  }
}
document.write(b);