HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Check if two JavaScript arrays have same contents

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
javascriptsamechecktwocontentshavearrays

Problem

Have you ever wanted to compare the contents of two arrays to see if they contain the same elements regardless of order? While this sounds like a fairly easy task, implementing it efficiently can be a little tricky.
Starting with the most naive solution, you can simply iterate over the first array and check if each element is present the same amount of times in the second array. You can use Array.prototype.every() and Array.prototype.filter() to achieve this.
This approach is inefficient, as we might end up calculating the frequency of the same element in each array multiple times. We can minimize this by using a Set to store the unique values of both arrays and then comparing the frequency of each value in both arrays.
This small change can net us a significant performance improvement, as we only calculate the frequency of each value once. It also ensures that we only iterate over the unique values of both arrays, which can be a huge performance boost for large arrays.

Solution

const haveSameContents = (a, b) =>
  a.length === b.length &&
  a.every(v => a.filter(e => e === v).length === b.filter(e => e === v).length);

haveSameContents([1, 2, 4], [2, 4, 1]); // true
haveSameContents([1, 1, 2, 3], [2, 1, 1, 3]); // true


This approach is inefficient, as we might end up calculating the frequency of the same element in each array multiple times. We can minimize this by using a Set to store the unique values of both arrays and then comparing the frequency of each value in both arrays.
This small change can net us a significant performance improvement, as we only calculate the frequency of each value once. It also ensures that we only iterate over the unique values of both arrays, which can be a huge performance boost for large arrays.

Code Snippets

const haveSameContents = (a, b) =>
  a.length === b.length &&
  a.every(v => a.filter(e => e === v).length === b.filter(e => e === v).length);

haveSameContents([1, 2, 4], [2, 4, 1]); // true
haveSameContents([1, 1, 2, 3], [2, 1, 1, 3]); // true
const haveSameContents = (a, b) =>
  a.length === b.length &&
  [...new Set([...a, ...b])].every(
    v => a.filter(e => e === v).length === b.filter(e => e === v).length
  );

haveSameContents([1, 2, 4], [2, 4, 1]); // true
haveSameContents([1, 1, 2, 3], [2, 1, 1, 3]); // true

Context

From 30-seconds-of-code: arrays-have-same-contents

Revisions (0)

No revisions yet.