# Get the difference between two sets using JavaScript

We will learn how to get the difference between two sets using JavaScript using two different methods: using filter() with has() methods and reusing a function. Each method is relatively straightforward, and you can choose whatever you want.

Table of Contents

## How to get the difference between two sets using JavaScript?

### Using filter() with has() method

The has() method returns a boolean indicating whether an element with the specified value exists in a Set object or not. The syntax of filter() has been introduced here.

Syntax:

has(value)

Parameter:

• value: The value to find in the Set object.

The has() method returns false if an element with the specified value does not exist in the Set object; otherwise, it returns true:

let set1 = new Set(['learn','share','it']);
let set2 = new Set(['it','learn']);

// Get an array of elements that are in set1 but not in set2
let array = [...set1].filter(e => !set2.has(e))
console.log(array)

// Convert an array into a set of differences between set1 from set2
let setOfDifferent = new Set(array)
console.log(setOfDifferent)

Output:

[ 'share']
{'share'}

The above example uses the has() method of array type to check if a value already exists in the set. It can be seen from the example that we have got a difference between set1 from set2. That means we care about what is in the first set but not in the second one. If you want to do it reversely, you can do as follow:

let set1 = new Set(['learn','share','it']);
let set2 = new Set(['it','learn']);

// Get an array of elements that are in set2 but not in set1
let array = [...set2].filter(e => !set1.has(e))
console.log(array)

// Convert an array into a set of differences between set2 from set1
let setOfDifferent = new Set(array)
console.log(setOfDifferent)

Output:

[]
{}

Do you see any difference between the two outputs? In this example, we have selected the elements in set 2 but not in set 1. However, the result will be empty as all the elements in set2 are in set1. This means that set 2 is the subset of set 1. In the next section, we will help you reduce the amount of code by using a function.

### Reusing code with a function

We can create a function based on the logic of the first approach to make our code more simple and reusable.

function getDifferenceBetweenTwoSets(set1,set2){
// Get an array of elements that are in set1 but not in set2
let array = [...set1].filter(e => !set2.has(e))

// Convert an array into a set of differences between set1 from set2
return new Set(array)
}


Sample test case:

set1 = new Set(['0','1','2']);
set2 = new Set(['3','2','1']);
console.log(getDifferenceBetweenTwoSets(set1,set2))

Output:

{'0'}

Another test case:

set1 = new Set(['0','1','2']);
set2 = new Set(['3','2','1']);
console.log(getDifferenceBetweenTwoSets(set2,set1))

Output:

{'3'}

As you can see, the logic behind this function is precisely based on the approach in the first solution. That is because no built-in functions can help you immediately get the difference between two sets in JavaScript. Therefore, the only approach we have is to use the filter() and has() method to check and sort out all the values in the first set but not in the second one.

## Summary

We have learned how to get the difference between two sets using JavaScript with two methods. The second solution is based on the first one but will consume less time and become more efficient. We hope you will be successful using our tutorial.

Maybe you are interested: