Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chris-burkhardt/1d192802e056102704504193f1fa59c8 to your computer and use it in GitHub Desktop.
Save chris-burkhardt/1d192802e056102704504193f1fa59c8 to your computer and use it in GitHub Desktop.
Remove Array Duplicates with new Set() in JS
const testArr1 = ['pig', 'fox', 'zebra']
const testArr2 = ['ferrari', 'fox', 'pig', 'maserati']
// each value of a Set needs to be unique, so combining arrays that have
// duplicate entries into a Set will auto-remove exact matches
const removeArrayDuplicates = (first, second) => {
const combinedArrays = [...first, ...second];
const arraySet = [...new Set(combinedArrays)]
return arraySet
}
console.log(removeArrayDuplicates(testArr1, testArr2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment