Array of Doubled Pairs LeetCode solution in JavaScript

1
var canReorderDoubled = function (arr) {
2
const hashMap = arr.reduce((map, num) => {
3
const count = map.get(num) ?? 0;
4
return map.set(num, count + 1);
5
}, new Map());
6

7
const nums = [...hashMap.keys()].sort((a, b) => {
8
return a < 0 && b < 0 ? b - a : a - b;
9
});
10

11
for (const num of nums) {
12
const count = hashMap.get(num);
13
const pairsCount = hashMap.get(num * 2);
14
if (count > pairsCount) return false;
15
if (count > 0 && pairsCount === undefined) return false;
16

17
hashMap.set(num * 2, pairsCount - count);
18
}
19
return true;
20
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0