# LeetCode: Summary Ranges Solution

Straight forward checking

## Approach

On each iteration, init

start
and
end
to be equal to the iterated element

Keep assigning

end
while next element is greater than the current by one unit

If

start !== end
then it's a sub range

## Implementation

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}var summaryRanges = function (nums) {2  let start, end3  let res = []4
5  for (let i = 0; i < nums.length; i++) {6    start = end = nums[i]7
8    for (; nums[i + 1] === nums[i] + 1; i++) {9      end = nums[i + 1]10    }11
12    res.push(start === end ? "" + start : start + "->" + end)13  }14
15  return res16}```

leetcode

array

## Next Post

LeetCode: Contains Duplicate II

Aug 31, 2021

Tricky

## Previous Post

LeetCode: Majority Element

Aug 30, 2021

Boyer-Moore Voting Algorithm

Search Posts