Hack Frontend Community

Compress Number List into Ranges

VK
Given a list of integers, there are no duplicate elements in the list. You need to write a function compress(list) that converts this list into a string, collapsing consecutive numbers in the sequence into ranges.

Examples:

Input 1: [-10, -8, -7, -6]
Output 1: "-10,-8--6"
Input 2: [1, 4, 5, 2, 3, 9, 8, 11, 0]
Output 2: "0-5,8-9,11"
Input 3: [Number.MIN_SAFE_INTEGER, Number.MIN_SAFE_INTEGER + 1]
Output 3: "-9007199254740991--9007199254740990"
Run your code to see results.