Not sure if in JS sorting isn't maybe even slower than a naive write and read approach:
const mergeIntervals = (intervals) => intervals .reduce((set, [start, end]) => { for (let index = start; index <= end; index++) { set[index] = true } return set }, []) .reduce((merged, on, index, set) => { const last = set[index - 1] if (on && last) { merged[merged.length - 1][1] = index } else if (on && !last) { merged.push([index, index]) } return merged }, [])
Warning: array methods will skip empty parts of sparse arrays, which is why last cannot be taken from the last operation.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Not sure if in JS sorting isn't maybe even slower than a naive write and read approach:
Warning: array methods will skip empty parts of sparse arrays, which is why last cannot be taken from the last operation.