Efficient Intersection of Arrays: Comparing Various Algorithms

Efficient Intersection of Arrays: Comparing Various Algorithms

Finding common elements between two arrays is a common task in data processing. This article compares different algorithms for this task, highlighting their pros and cons. The goal is to understand the trade-offs and choose the most suitable method based on specific requirements.

1. Hash-Based Intersection

Algorithm:

function commonElement(arr1, arr2) {
    const elementMap = new Map();

    arr1.forEach(element => {
        elementMap.set(element, (elementMap.get(element) || 0) + 1);
    });

    const commonElements = [];
    arr2.forEach(element => {
        if (elementMap.has(element) && elementMap.get(element) > 0) {
            commonElements.push(element);
            elementMap.set(element, elementMap.get(element) - 1);
        }
    });

    commonElements.sort((a, b) => a - b);
    return commonElements;
}        

Pros:

  • Time Complexity: Average-case O(n)O(n)O(n) for finding common elements and O(n log n) O(n log n) O(n log n) for sorting.
  • Handles Duplicates: Properly accounts for duplicate elements in both arrays.

Cons:

  • Space Complexity: Requires additional space proportional to the number of unique elements in the first array.

2. Set-Based Intersection

Algorithm:

function commonElement(arr1, arr2) {
    const set1 = new Set(arr1);
    const set2 = new Set(arr2);
    const commonElements = [...set1].filter(element => set2.has(element));
    return commonElements;
}        

Pros:

  • Time Complexity: O(n)O(n)O(n) for average-case performance in Set operations.
  • Simplicity: Code is simpler and easier to understand, especially when duplicates are not needed.

Cons:

  • No Duplicate Handling: Does not handle duplicate elements; only unique elements are considered.
  • Space Complexity: Requires additional space proportional to the number of unique elements in both arrays.

3. Single Pass with HashSet

Algorithm:

function commonElement(arr1, arr2) {
    const set1 = new Set(arr1);
    const commonElements = new Set();

    arr2.forEach(element => {
        if (set1.has(element)) {
            commonElements.add(element);
        }
    });

    return Array.from(commonElements);
}        

Pros:

  • Time Complexity: O(n)O(n)O(n) for average-case performance in Set operations.
  • Handles Unique Elements Efficiently: Suitable for cases where duplicates in the result are not needed.

Cons:

  • No Duplicate Handling: Similar to the Set-Based Intersection, duplicates are not managed in the results.
  • Space Complexity: Requires space for storing unique elements in both arrays.

4. Sorting and Two-Pointer Technique

Algorithm:

function commonElement(arr1, arr2) {
    arr1.sort((a, b) => a - b);
    arr2.sort((a, b) => a - b);

    let i = 0, j = 0;
    const commonElements = [];

    while (i < arr1.length && j < arr2.length) {
        if (arr1[i] < arr2[j]) {
            i++;
        } else if (arr1[i] > arr2[j]) {
            j++;
        } else {
            commonElements.push(arr1[i]);
            i++;
            j++;
        }
    }

    return commonElements;
}        

Pros:

  • Effective for Sorted Data: Optimal when arrays are already sorted or can be sorted efficiently.
  • Time Complexity: O(nlogn)O(n \log n)O(nlogn) for sorting and O(n)O(n)O(n) for the two-pointer scan, making it efficient for large datasets.

Cons:

  • Sorting Overhead: Sorting introduces O(nlogn)O(n \log n)O(nlogn) complexity, which might be an overhead if sorting is expensive.
  • In-Place Sorting Required: Requires sorting the arrays, which could be an issue if the original order needs to be preserved.

Choosing the right approach depends on the specific requirements, such as the need to handle duplicates, the size of the arrays, and whether sorting is feasible. Each method has its advantages and trade-offs, and understanding these can help in selecting the most appropriate algorithm for a given scenario.

To view or add a comment, sign in

More articles by Mohideen Risvi.Y

Others also viewed

Explore content categories