Skip to content

Home

Check if arrays have same contents

Checks if two arrays contain the same elements regardless of order.

const haveSameContents = (a, b) => {
  for (const v of new Set([...a, ...b]))
    if (a.filter(e => e === v).length !== b.filter(e => e === v).length)
      return false;
  return true;
};

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

More like this

Start typing a keyphrase to see matching snippets.