Get the Difference between Two Sets using JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
2 min

banner

# Get the Difference between Two Set objects in JavaScript

To get the difference between two Sets:

  1. Convert the first Set to an Array.
  2. Use the filter() method to iterate over the array.
  3. Use the has() method to check if each element is not contained in the second Set.
  4. Convert the array back to a Set.
index.js
function getDifference(setA, setB) { return new Set( [...setA].filter(element => !setB.has(element)) ); } const set1 = new Set(['a', 'b', 'c']); const set2 = new Set(['a', 'b']); console.log(getDifference(set1, set2)); // ๐Ÿ‘‰๏ธ {'c'}

get difference between two set objects

The code for this article is available on GitHub

We used the spread syntax (...) to convert the first Set to an array, so we can use the Array.filter() method.

The function we passed to the Array.filter() method gets called with each element in the array.

The filter() method returns a new array that only contains the elements that meet the condition.

On each iteration, we check if the current element is not contained in the second Set by negating the result of the Set.has() method.

index.js
function getDifference(setA, setB) { return new Set( [...setA].filter(element => !setB.has(element)) ); }

The has() method returns true if the element is contained in the Set, and false otherwise.

# Get the complete Difference between Two Set objects

However, this doesn't return the complete difference between the Set objects because we only check if the elements of the first Set are not contained in the second Set.

We didn't check if the elements of the second Set are not contained in the first.

index.js
function getDifference(setA, setB) { return new Set( [...setA].filter(element => !setB.has(element)) ); } const set1 = new Set(['a']); const set2 = new Set(['a', 'b', 'c']); console.log(getDifference(set1, set2)); // ๐Ÿ‘‰๏ธ {}

getting complete difference incorrectly

The code for this article is available on GitHub

We got an empty Set for the difference where a Set containing {'b', 'c'} would have been expected.

We only iterated over the first Set, which has 1 element, so we didn't get the complete difference.

To solve this issue, we need to call the getDifference() method two times and combine the results.

index.js
function getDifference(setA, setB) { return new Set( [...setA].filter(element => !setB.has(element)) ); } const set1 = new Set(['a']); const set2 = new Set(['a', 'b', 'c']); const difference = new Set([ ...getDifference(set1, set2), ...getDifference(set2, set1), ]); console.log(difference); // ๐Ÿ‘‰๏ธ {'b', 'c'}

getting complete difference correctly

The code for this article is available on GitHub

Here's what we did to get this working:

  1. Iterate over the elements of the first Set and return only the elements that are not contained in the second Set.
  2. Iterate over the elements of the second Set and return only the elements that are not contained in the first.
  3. Combine the results into a third Set.

Now, our example contains the complete difference between the two Set objects.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev