Skip to content

Instantly share code, notes, and snippets.

@jasonseminara
Created January 15, 2019 17:18
Show Gist options
  • Save jasonseminara/fc37556e09ca502477512656a17b88c1 to your computer and use it in GitHub Desktop.
Save jasonseminara/fc37556e09ca502477512656a17b88c1 to your computer and use it in GitHub Desktop.
/**
* one-line solution with sets!
*/
const oneliner = [...new Set(colors.split(' '))].join(' ');
console.log(oneliner);
/**
* WHOAH! let's break that down!
*/
// break up the string on spaces
const colorArray = colors.split(' ');
// use the Set constructor to convert the array into a non-repeating set
// @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#Description
const colorSet = new Set(colorArray);
// we want to destructure the set into an array then join into a string
const solution3 = [...colorSet].join(' ');
console.log(solution3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment