Hand of Straights LeetCode solution in JavaScript

1
var isNStraightHand = function (hand, groupSize) {
2
if (hand.length % groupSize !== 0) return false;
3

4
const map = new Map();
5
hand.forEach((h) => {
6
map.set(h, map.get(h) + 1 || 1);
7
});
8

9
// sort based on the key in asc order
10
const sortedMap = new Map([...map.entries()].sort((a, b) => a[0] - b[0]));
11

12
while (sortedMap.size) {
13
// getting the first key
14
const firstKey = sortedMap.keys().next().value;
15
for (let i = firstKey; i < firstKey + groupSize; ++i) {
16
if (!sortedMap.has(i)) {
17
return false;
18
}
19
const freq = sortedMap.get(i);
20
if (freq === 1) {
21
sortedMap.delete(i);
22
} else {
23
sortedMap.set(i, sortedMap.get(i) - 1);
24
}
25
}
26
}
27
return true;
28
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0