Given an array of integers return positives, whose equivalent negatives present in it in JavaScript

In the given problem statement we are presented with an array which contains positive and negative values. So our task is to keep the positive once and filter out the negative numbers from the array. And implement the solution in Javascript.

Understanding the Problem

We will be given an array of integer numbers and we need to find the positive integers which have their respective negative integers present in the array. For example suppose we have an array of integers like [7, -7, 8, -8, 9, -9], in this array we can see that their is some positive integers and some negative numbers present so our task in this problem is to filter this array with keeping only positive number and filter out the negative numbers from the array so the output or the array will be [7, 8, 9]. So basically we will implement the program to do this operation.

Logic for the given Problem

The problem statement is stating that we have to extract the positive integers from the array which have corresponding negative integers within an array. So we will iterate over the given array of numbers. For every item in the array we will follow certain steps. If the item is positive in the iteration then we will add it to a set. This set will store all the positive numbers. And if the item is negative then we will check that its absolute value exists in the set; if it is present that means the positive counterpart of this negative number is present in the array. So We shall add the exact value to the result array. And lastly we will return the result containing the positive integers which have their respective negative values.


Step 1: First, define a function to find the positive of the respective negatives present in the array. And give this function a name as findPositives and this function will take an argument as an array.

Step 2: Create a set to store the positive sets of the array. And also define a blank array for storing the result.

Step 3: Then we will iterate through the array of integers. And we will check some conditions for every item in the array. First condition is to check that the item is positive and greater than 0. If the condition is true then we will add it to a set called positiveSet. This variable will be used to store all the positive integers present in the array.

Step 4: Check whether the item is negative or less than 0. So determining its absolute value is present in the positive set. If the condition is true, it shows that the positive number of the negative number present in the array. So add that value to the result array.

Step 5: Lastly, return the result of the positive values on the array with respect to their negative values.


//Function to find the positives into the array with their respective negatives
function findPositives(arr) {
   const positiveSet = new Set();
   const result = [];
   for (let num of arr) {
   if (num > 0) {
   } else if (num < 0 && positiveSet.has(Math.abs(num))) {
    return result;
const array = [1, -1, 2, -2, 3, -4, 4, 5, -5];
const positives = findPositives(array);


[ 1, 2, 5 ]


As the code traverses through the array once in the loop and performs the constant time operation for every item. So the time complexity for the code is O(n) in which n is the size of the input array. The space complexity for the code is also O(n).


The program effectively finds a solution to get the positive integers which have their respective negative values available in the array. As we have traversed over the array and maintained a set of positive integers to check its corresponding negative integers. Overall we can say that this is a straightforward solution for the given problem with a linear time complexity.

Updated on: 14-Aug-2023


Kickstart Your Career

Get certified by completing the course

Get Started