Menu

gitpiper

symmetricDifference javascript Code Snippet in 2024

arraymathintermediate

Last Updated: 20 April 2024

Returns the symmetric difference between two arrays, without filtering out duplicate values.

  • Create a new Set() from each array to get the unique values of each one.
  • Use Array.prototype.filter() on each of them to only keep values not contained in the other.
const symmetricDifference = (a, b) => { const sA = new Set(a), sB = new Set(b); return [...a.filter(x => !sB.has(x)), ...b.filter(x => !sA.has(x))]; };
symmetricDifference([1, 2, 3], [1, 2, 4]); // [3, 4] symmetricDifference([1, 2, 2], [1, 3, 1]); // [2, 2, 3]

javascript snippet similar to symmetricDifference For You in April 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️