Created
February 25, 2026 19:13
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // 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