Skip to content

Instantly share code, notes, and snippets.

@petergi
Created February 25, 2026 19:13
Show Gist options
  • Select an option

  • Save petergi/ba070cb95d2357117a3bf71f2523d98d to your computer and use it in GitHub Desktop.

Select an option

Save petergi/ba070cb95d2357117a3bf71f2523d98d to your computer and use it in GitHub Desktop.
Creates an array with the unique values filtered out. - Use the `Set` constructor and the spread operator (`...`) to create an array of the unique values in `arr`. - Use `Array.prototype.filter()` to create an array containing only the non-unique values.
//
// Creates an array with the unique values filtered out.
//
// - Use the `Set` constructor and the spread operator (`...`) to create an array of the unique values in `arr`.
// - Use `Array.prototype.filter()` to create an array containing only the non-unique values.
const filterUnique = arr =>[...new Set(arr)].filter(i => arr.indexOf(i) !== arr.lastIndexOf(i));
filterUnique([1, 2, 2, 3, 4, 4, 5]); // [2, 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment