Non-negative set subtraction in JavaScript

In the given problem statement we are presented with two arrays which contain integer values. So our aim is to find the non negative set subtraction from the two arrays. And implement the solution in Javascript. We can perform this task using the Set object and forEach method.

What are the Set object and forEach method in Javascript ?

Set object in Javascript

The Set is an object in Javascript which is a built in data Structure introduced in ES6. This allows us to store the unique values of any type. The values can be primitive values or object references. The Set object gives methods for adding, removing or querying items in a set.

The key characteristics of the Set object are: First is uniqueness, the set contains only unique items and duplicate values are automatically eliminated. Second is Iteration, items in the Set are ordered as per the insertion order. Third is reference quality, Set uses reference equality at the time of comparing object values.

The forEach method in Javascript

The forEach method is also a built-in function available to work on the Set object as well as on other objects in Javascript. This function allows us to traverse over the items of a Set and perform a specified action on every item. The forEach method accepts a callback function as an argument and calls that function for every item in the set. For example:


var mySet = new Set();


mySet.forEach(function(value) {



Understanding the problem

In the given problem we have to find the non negative set subtraction in which we need to remove the items from one set that are also present in another set. In this problem we will perform non negative set subtraction. That means we have to subtract items from one set at the same time we will be ensuring the resulting set contains only non negative values.

Logic for the given problem

To solve the given problem of non negative set subtraction in Javascript we can use the built in Set object and use its methods and properties. Then we will create a function which takes two parameters of sets as input. And then it will return the new set which contains the items from the first set which are not present in the second set and also exclude any negative values.


Step 1: Create a function called nonNegativeSet which takes two parameters of sets named setA and setB. This function will create a new set from the given sets.

Step 2: Then we will create a blank Set object and name it as resultSet to keep the values of result.

Step 3: Iterate over the items of setA with the help of forEach method.

Step 4: We will check that the items of setA are not present in setB with the usage of has method. If the items are not present then add them in a new set which is the required result.


function nonNegativeSet(setA, setB) {
   // Create a new set to store the result
   var resultSet = new Set();

   // Iterate over the elements of setA
   setA.forEach(function (element) {
      // Check if the element is not in setB
      if (!setB.has(element)) {
         // Add the element to the result set

   return resultSet;

var set1 = new Set([1, 2, 3, 4, 5]);
var set2 = new Set([3, 4, 5, 6, 7]);
var theSet = nonNegativeSet(set1, set2);


Set { 1, 2 }


The time complexity for the code snippet depends on the number of items in the Set. And if we add an item has an average time complexity of O(1) and traversing over the Set with the help of forEach has a time complexity of O(n).


So in the above solution we have successfully created an algorithm to create a non negative set subtraction with the help of some built-in functionalities of Javascript. The Set and forEach method are very useful techniques in Javascript for arranging the collections of unique values and performing operations on them in an effective and convenient way.

Updated on: 16-Aug-2023


Kickstart Your Career

Get certified by completing the course

Get Started