I'm tying to solve an "easy" problem from LeetCode, called Divide Array in Sets of K Consecutive Numbers, but can not find a way of how to check the sequences. From my point of view it will be to many loops:
const isPossibleDivide = (nums, k) => {
const sorted = nums.sort((a, b) => a - b)
const counts = {}
sorted.forEach(item => counts[item] = (counts[item] || 0) + 1)
// the problem part
Object.entries(counts).map(([key, value]) => {
if (value !== 0) {
counts[key] = value - 1
}
})
console.log(counts)
}
isPossibleDivide([3, 2, 1, 2, 3, 4, 3, 4, 5, 9, 10, 11], 3)