Comparing corresponding values of two arrays in JavaScript

When working with arrays in JavaScript, you might need to compare corresponding elements to determine which array has more "dominant" values at each position. This article demonstrates how to build a function that compares two arrays element by element and returns a result indicating which array has more greater values.

Problem Statement

Given two arrays of equal length, we need to compare corresponding elements and return:

  • -1 if the first array has more elements greater than their corresponding elements in the second array

  • 1 if the second array has more elements greater than their corresponding elements in the first array

  • 0 if both arrays have an equal count of greater corresponding elements

Example Arrays

const arr1 = [23, 67, 12, 87, 33, 56, 89, 34, 25];
const arr2 = [12, 60, 45, 54, 67, 84, 36, 73, 44];

console.log("Array 1:", arr1);
console.log("Array 2:", arr2);
Array 1: [ 23, 67, 12, 87, 33, 56, 89, 34, 25 ]
Array 2: [ 12, 60, 45, 54, 67, 84, 36, 73, 44 ]

Solution Implementation

const arr1 = [23, 67, 12, 87, 33, 56, 89, 34, 25];
const arr2 = [12, 60, 45, 54, 67, 84, 36, 73, 44];

const findDominance = (arr1 = [], arr2 = []) => {
    // Check if arrays have equal length
    if(arr1.length !== arr2.length){
        return null; // Return null for invalid input
    }
    
    let count = 0;
    
    for(let i = 0; i < arr1.length; i++){
        const el1 = arr1[i];
        const el2 = arr2[i];
        const diff = el2 - el1;
        
        // Add 1 if arr2 element is greater, -1 if arr1 element is greater
        if(diff !== 0) {
            count += diff / Math.abs(diff);
        }
    }
    
    // Return the sign of the total count
    if(count === 0) return 0;
    return count / Math.abs(count);
};

console.log("Result:", findDominance(arr1, arr2));
Result: 1

How It Works

The algorithm works by:

  1. Validation: First checks if both arrays have the same length

  2. Element-wise comparison: Iterates through each index and calculates the difference between corresponding elements

  3. Score calculation: For each comparison, adds +1 if the second array element is greater, -1 if the first array element is greater

  4. Result normalization: Returns the sign of the total score (-1, 0, or 1)

Detailed Step-by-Step Analysis

const arr1 = [23, 67, 12, 87, 33, 56, 89, 34, 25];
const arr2 = [12, 60, 45, 54, 67, 84, 36, 73, 44];

const analyzeComparison = (arr1, arr2) => {
    let arr1Wins = 0;
    let arr2Wins = 0;
    
    console.log("Index | Arr1 | Arr2 | Winner");
    console.log("------|------|------|-------");
    
    for(let i = 0; i < arr1.length; i++) {
        const winner = arr1[i] > arr2[i] ? "Arr1" : arr2[i] > arr1[i] ? "Arr2" : "Tie";
        console.log(`  ${i}   |  ${arr1[i]}  |  ${arr2[i]}  | ${winner}`);
        
        if(arr1[i] > arr2[i]) arr1Wins++;
        else if(arr2[i] > arr1[i]) arr2Wins++;
    }
    
    console.log(`\nArr1 wins: ${arr1Wins} positions`);
    console.log(`Arr2 wins: ${arr2Wins} positions`);
    
    return arr2Wins > arr1Wins ? 1 : arr1Wins > arr2Wins ? -1 : 0;
};

console.log("Final result:", analyzeComparison(arr1, arr2));
Index | Arr1 | Arr2 | Winner
------|------|------|-------
  0   |  23  |  12  | Arr1
  1   |  67  |  60  | Arr1
  2   |  12  |  45  | Arr2
  3   |  87  |  54  | Arr1
  4   |  33  |  67  | Arr2
  5   |  56  |  84  | Arr2
  6   |  89  |  36  | Arr1
  7   |  34  |  73  | Arr2
  8   |  25  |  44  | Arr2

Arr1 wins: 4 positions
Arr2 wins: 5 positions
Final result: 1

Alternative Approach

Here's a more readable version using array methods:

const compareArrays = (arr1, arr2) => {
    if(arr1.length !== arr2.length) return null;
    
    const comparisons = arr1.map((val, index) => {
        if(val > arr2[index]) return -1;  // arr1 wins
        if(val < arr2[index]) return 1;   // arr2 wins
        return 0;  // tie
    });
    
    const totalScore = comparisons.reduce((sum, score) => sum + score, 0);
    
    return totalScore === 0 ? 0 : Math.sign(totalScore);
};

console.log("Alternative result:", compareArrays(arr1, arr2));
Alternative result: 1

Conclusion

Comparing corresponding array elements is useful for determining which dataset has more dominant values. The function returns 1 when the second array wins more comparisons, -1 when the first array dominates, and 0 for ties.

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

438 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements