Comparing array elements keeping count in mind in JavaScript

Suppose, we have two arrays of literals that contain the same number of elements. We are supposed to write a function that checks whether or not the both arrays contain the same elements appearing for the same number of times.

If the arrays fulfil this condition, we return true, false otherwise.

We will create a copy of the second array, and start iterating over the first array. As we iterate, we will keep deleting the elements from the second array that are present in first array. If during iteration we encounter any element that isn't present in second array, we return false. Otherwise, at the end of iteration we will return true.

Method 1: Using Array Methods (Splice and Every)

This approach creates a copy of the second array and removes matching elements during iteration:

const arr1 = [2, 5, 7, 4, 3, 3];
const arr2 = [3, 5, 7, 2, 3, 4];

const compareWithCount = (arr1, arr2) => {
    if(arr1.length !== arr2.length){
        return false;
    };
    const copy2 = arr2.slice();
    const areEqual = arr1.every(el => {
        if(!copy2.includes(el)){
            return false;
        };
        copy2.splice(copy2.indexOf(el), 1);
        return true;
    });
    return areEqual;
};

console.log(compareWithCount(arr1, arr2));
console.log(compareWithCount([1, 2, 3], [1, 2, 2])); // Different counts
true
false

Method 2: Using Frequency Count Objects

A more efficient approach using objects to count element frequencies:

const compareWithFrequency = (arr1, arr2) => {
    if (arr1.length !== arr2.length) {
        return false;
    }
    
    // Count frequencies in first array
    const freq1 = {};
    for (let el of arr1) {
        freq1[el] = (freq1[el] || 0) + 1;
    }
    
    // Count frequencies in second array
    const freq2 = {};
    for (let el of arr2) {
        freq2[el] = (freq2[el] || 0) + 1;
    }
    
    // Compare frequency objects
    for (let key in freq1) {
        if (freq1[key] !== freq2[key]) {
            return false;
        }
    }
    
    return true;
};

const arr3 = [1, 2, 2, 3];
const arr4 = [2, 1, 3, 2];
const arr5 = [1, 2, 3];

console.log(compareWithFrequency(arr3, arr4)); // Same elements, same count
console.log(compareWithFrequency(arr3, arr5)); // Different lengths
true
false

Comparison

Method Time Complexity Space Complexity Readability
Splice Method O(n²) O(n) Good
Frequency Count O(n) O(n) Better

Key Points

  • Always check array lengths first for quick optimization
  • The splice method modifies the array during iteration
  • Frequency counting is more efficient for large arrays
  • Both methods handle duplicate elements correctly

Conclusion

Both methods effectively compare arrays while considering element frequency. The frequency count approach is more efficient for larger datasets, while the splice method offers simpler logic for smaller arrays.

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

195 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements