Skip to content

Instantly share code, notes, and snippets.

@cferdinandi
Created July 5, 2023 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cferdinandi/e96773b49e9b07b8355a9ef84eabe883 to your computer and use it in GitHub Desktop.
Save cferdinandi/e96773b49e9b07b8355a9ef84eabe883 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>dedupe()</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<script>
/**
* Remove duplicate items from an array
* (c) Chris Ferdinandi, MIT License, https://gomakethings.com
* @param {Array} arr The array
* @return {Array} A new array with duplicates removed
*/
function dedupe (arr) {
return Array.from(new Set(arr));
}
let sandwiches = ['turkey', 'ham', 'turkey', 'tuna', 'pb&j', 'ham', 'turkey', 'tuna'];
// returns ["turkey","ham","tuna","pb&j"]
let deduped = dedupe(sandwiches);
console.log(deduped);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment