theonlychase
4/23/2019 - 11:26 PM

Remove duplicates from an array

Keep same order while removing the duplicate

// ES6 Set
function removeDups(arr) {
	let noDup = [...new Set(arr)];
	return noDup;
}
removeDups(["John", "Taylor", "John"]) // ["John", "Taylor"]

// Reduce
function removeDups(arr) {
	let uniq = arr.reduce((a,b) => {
      if (a.indexOf(b) < 0) a.push(b);
      return a;
    },[])
    console.log(uniq)
}
removeDups(["John", "Taylor", "John"]) // ["John", "Taylor"]