Common element with least index sum in JavaScript

We need to find common elements between two arrays that have the smallest sum of their indices from both arrays. When multiple elements tie for the smallest sum, we return all of them.

Problem Understanding

Given two arrays, for each common element, we calculate the sum of its index in the first array and its index in the second array. The element(s) with the minimum index sum are returned.

For example, with arrays:

const arr1 = ['a', 'b', 'c', 'd'];
const arr2 = ['d', 'a', 'c'];

Common elements and their index sums:

  • 'd': index 3 in arr1 + index 0 in arr2 = 3
  • 'a': index 0 in arr1 + index 1 in arr2 = 1
  • 'c': index 2 in arr1 + index 2 in arr2 = 4

Minimum sum is 1, so 'a' is returned.

Solution

const arr1 = ['a', 'b', 'c', 'd'];
const arr2 = ['d', 'a', 'c'];

const findCommon = (arr1 = [], arr2 = []) => {
    let minSum = Infinity;
    
    // Create a map of elements to their indices in arr1
    const map = arr1.reduce((acc, str, index) => {
        acc[str] = index;
        return acc;
    }, {});
    
    // Find the minimum index sum among common elements
    for (let i = 0; i < arr2.length; i++) {
        const index1 = map[arr2[i]];
        if (index1 >= 0 && index1 + i < minSum) {
            minSum = index1 + i;
        }
    }
    
    // Collect all elements with the minimum sum
    const result = [];
    for (let i = 0; i < arr2.length; i++) {
        const index1 = map[arr2[i]];
        if (index1 >= 0 && index1 + i === minSum) {
            result.push(arr2[i]);
        }
    }
    
    return result;
};

console.log(findCommon(arr1, arr2));
['a']

Example with Multiple Results

When multiple elements have the same minimum index sum:

const arr1 = ['x', 'y', 'z'];
const arr2 = ['z', 'y', 'x'];

const findCommon = (arr1 = [], arr2 = []) => {
    let minSum = Infinity;
    
    const map = arr1.reduce((acc, str, index) => {
        acc[str] = index;
        return acc;
    }, {});
    
    for (let i = 0; i < arr2.length; i++) {
        const index1 = map[arr2[i]];
        if (index1 >= 0 && index1 + i < minSum) {
            minSum = index1 + i;
        }
    }
    
    const result = [];
    for (let i = 0; i < arr2.length; i++) {
        const index1 = map[arr2[i]];
        if (index1 >= 0 && index1 + i === minSum) {
            result.push(arr2[i]);
        }
    }
    
    return result;
};

console.log(findCommon(arr1, arr2));
console.log("Index sums: z(2+0=2), y(1+1=2), x(0+2=2)");
['z', 'y', 'x']
Index sums: z(2+0=2), y(1+1=2), x(0+2=2)

How It Works

  1. Create index map: Store each element from arr1 with its index
  2. Find minimum sum: Calculate index sum for each common element
  3. Collect results: Return all elements that match the minimum sum

Conclusion

This algorithm efficiently finds common elements with the least index sum using a hash map for O(1) lookups. The time complexity is O(m + n) where m and n are the array lengths.

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

244 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements