code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

228 Summary Ranges

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

const summaryRanges = nums => {
  let result = []
  let right = 0
  let counter = 0
  while (right < nums.length) {
    if (nums[right + 1] === nums[right] + 1) {
      right++
    } else {
      if (counter === right) {
        result.push(nums[counter])
      } else {
        result.push(`${nums[counter]}->${nums[right]}`)
      }
      counter = ++right
    }
  }
  return result
}

console.log(summaryRanges([0, 1, 2, 4, 5, 7]))

Created 2023-05-13T15:57:52+01:00 · Edit