Skip to content

Instantly share code, notes, and snippets.

@manzaloros
Last active October 28, 2020 15:48
Show Gist options
  • Save manzaloros/013fe9bb832dac7ae1cdabba34b0d0c3 to your computer and use it in GitHub Desktop.
Save manzaloros/013fe9bb832dac7ae1cdabba34b0d0c3 to your computer and use it in GitHub Desktop.
summaryRanges.js
const summaryRanges = (nums, [beginning, end] = [nums[0]]) => {
return nums.reduce((result, curr, i) => {
const next = nums[i + 1];
if (curr + 1 !== (next)) {
result.push(end === beginning ? `${end}` : `${beginning}->${end}`);
beginning = next;
}
end = next;
return result;
}, []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment