Preview:
  function summaryRanges(nums: number[]): string[] {
    if (nums.length === 0) return [];

    let answer: string[] = [];
    let start = nums[0];

    for (let i = 1; i <= nums.length; i++) {
      if (i === nums.length || nums[i] !== nums[i - 1] + 1) {
        if (start === nums[i - 1]) {
          answer.push(`${start}`);
        } else {
          answer.push(`${start}->${nums[i - 1]}`);
        }

        if (i < nums.length) {
          start = nums[i];
        }
      }
    }

    return answer;
  }
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter