Array of Doubled Pairs LeetCode solution in JavaScript
1var canReorderDoubled = function (arr) {2const hashMap = arr.reduce((map, num) => {3const count = map.get(num) ?? 0;4return map.set(num, count + 1);5}, new Map());67const nums = [...hashMap.keys()].sort((a, b) => {8return a < 0 && b < 0 ? b - a : a - b;9});1011for (const num of nums) {12const count = hashMap.get(num);13const pairsCount = hashMap.get(num * 2);14if (count > pairsCount) return false;15if (count > 0 && pairsCount === undefined) return false;1617hashMap.set(num * 2, pairsCount - count);18}19return true;20};