Skip to main content

Compress Number List to Ranges

MIDDLE

Compress Number List to Ranges

Convert a sorted list of numbers into ranges.

Requirements:

  • Group consecutive numbers into ranges
  • Format: "start-end" for ranges, or just "num" for singles
  • Input is sorted

Example:

Input: [1,2,3,5,7,8,9]
Output: ["1-3","5","7-9"]

Input: [0,1,2,4,5,7]
Output: ["0-2","4-5","7"]

Examples:

Input 1:{"nums":[1,2,3,5,7,8,9]}
Output 1:["1-3","5","7-9"]
Input 2:{"nums":[0,1,2,4,5,7]}
Output 2:["0-2","4-5","7"]

Loading editor...

Run your code to see results

Click the Run button above