Search code examples
javascriptgrouping

Create string ranges from sorted array of integers


I have following sorted array of numbers (they can repeat e.g 21)

let a = [1,2,3,4,7,8,12,15,21,21,22,23]

And want to get following (sorted) array of strings with ranges

let r =["1-4","7-8","12","15","21-23"]

for consecutive numbers a,a+1,a+2,...,a+n=b w must create string "a-b" e.g for 6,7,8 we want to get "6-8", for "alone" numbers we want to get only that number e.g. for 12 in above example we get "12".

Here is my attempt but I stuck on it (and get headache)

let a = [1,2,3,6,7,8,12,15,21,21,22,23];
let right=a[0];
let left=a[0];
let result=[];

for(let i=1; i<a.length; i++) {
    for(let j=1; j<a.length; j++) {
      if(a[i]<a[j])result.push(`${a[i]}-${a[j]}`);
      
  }
}

console.log(JSON.stringify(result));

Update:

Here is "inversion" of this question


Solution

  • You could store the next expected value in a closure and reduce the array.

    function getRanges(array) {
        return array.reduce((l => (r, v, i, a) => {
            if (l[1] > v) return r;
            r.push(l[1] === v
                ? (r.pop(), l.join('-'))
                : (l = [v, v]).slice(0, 1).toString()
            );
            l[1]++;
            return r;
        })([]), []);
    }
    
    console.log(getRanges([-3, -2, -1, 2]));
    console.log(getRanges([1, 2, 3, 4, 7, 8, 12, 15, 21, 21, 22, 23]));