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

In this problem, we need to find positive integers from an array that have their corresponding negative counterparts present in the same array. For example, if we have [1, -1, 2, -3, 4, -4], we should return [1, 4] since both 1 and 4 have their negatives (-1 and -4) in the array.

Understanding the Problem

Given an array of integers containing both positive and negative values, we need to identify positive numbers whose negative equivalents also exist in the array. For instance, in the array [7, -7, 8, -8, 9, -9], all positive numbers (7, 8, 9) have their corresponding negatives present, so our result would be [7, 8, 9].

Algorithm Approach

We'll use a two-pass approach with a Set data structure for efficient lookup:

  1. First pass: Store all positive numbers in a Set
  2. Second pass: For each negative number, check if its absolute value exists in the positive Set
  3. If found, add the positive value to our result array

Implementation

function findPositivesWithNegatives(arr) {
    const positiveSet = new Set();
    const result = [];
    
    // First pass: collect all positive numbers
    for (let num of arr) {
        if (num > 0) {
            positiveSet.add(num);
        }
    }
    
    // Second pass: find positives with corresponding negatives
    for (let num of arr) {
        if (num < 0 && positiveSet.has(Math.abs(num))) {
            result.push(Math.abs(num));
        }
    }
    
    return result;
}

// Test with example array
const array = [1, -1, 2, -2, 3, -4, 4, 5, -5];
const positives = findPositivesWithNegatives(array);

console.log("Original array:", array);
console.log("Positives with negatives:", positives);
Original array: [1, -1, 2, -2, 3, -4, 4, 5, -5]
Positives with negatives: [1, 2, 5]

Alternative Single-Pass Solution

We can optimize this to a single pass by checking conditions during iteration:

function findPositivesSinglePass(arr) {
    const positiveSet = new Set();
    const result = [];
    
    for (let num of arr) {
        if (num > 0) {
            positiveSet.add(num);
        } else if (num < 0 && positiveSet.has(Math.abs(num))) {
            result.push(Math.abs(num));
        }
    }
    
    return result;
}

// Test the single-pass version
const testArray = [7, -7, 8, -8, 9, -9, 10];
console.log("Result:", findPositivesSinglePass(testArray));
Result: [7, 8, 9]

Handling Edge Cases

// Test edge cases
console.log("Empty array:", findPositivesWithNegatives([]));
console.log("Only positives:", findPositivesWithNegatives([1, 2, 3]));
console.log("Only negatives:", findPositivesWithNegatives([-1, -2, -3]));
console.log("With zero:", findPositivesWithNegatives([0, 1, -1, 2]));
Empty array: []
Only positives: []
Only negatives: []
With zero: [1]

Complexity Analysis

Approach Time Complexity Space Complexity
Two-pass solution O(n) O(n)
Single-pass solution O(n) O(n)

Both approaches have linear time complexity since we traverse the array once (or twice), and Set operations (add, has) are O(1) on average. The space complexity is O(n) for storing positive numbers in the Set.

Conclusion

This problem efficiently demonstrates the use of Set data structure for fast lookups. The single-pass solution is slightly more efficient as it processes the array only once, making it ideal for large datasets where performance matters.

Updated on: 2026-03-15T23:19:00+05:30

610 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements