function haveSameValues(arr1, arr2) {
if (arr1.length !== arr2.length) {
return false;
}
const count = new Map;
arr1.forEach(n => count.set(n, -~count.get(n)));
arr2.forEach(n => count.set(n, ~-count.get(n)));
for (const n of count.values()) if (n) {
return false;
}
return true;
}
haveSameValues(
[ 'hello, world!!', 0, 0, 0, 1, 1, false, false ],
[ false, false, 1, 1, 0, 0, 0, 'hello, world!!' ]
) // true
haveSameValues(
[ 1, 2, 3 ],
[ 3, 2, 2 ]
) // false
haveSameValues(
[],
[]
) // true
const areEqual = (arrA, arrB) => {
if (arrA.length !== arrB.length) return false;
const a = arrA.slice().sort(), b = arrB.slice().sort();
return a.every((el, i) => el === b[i]);
}
const areEqual = (arrA, arrB) => {
if (arrA.length !== arrB.length) return false;
const a = arrA.slice(), b = arrB.slice();
while (a.length) {
const i = b.indexOf(a.pop());
if (-1 === i) return false;
b.splice(i, 1);
}
return true;
}