Skip to content Skip to sidebar Skip to footer

How Do I Merge Consecutive Numbers In A Sorted List Of Numbers?

I want to concatenate a sequence of numbers in a readable string. Consecutive numbers should be merged like this '1-4'. I'm able to concatenate an array with all the numbers into a

Solution 1:

In your code, lastValue never changes in the loop, so you're forever comparing against the first element in the array. Also, when you do find a match, you aren't yet ready to append to the pages result just yet--there might be more numbers to come.

One approach might be to keep a run of the current sequence of numbers (or just the first and last numbers in a run), and only append this run to the result string whenever we find a break in the sequence or hit the end of the string.

There are many ways to approach this, and I recommend checking other folks' answers at the Codewars: Range Extraction kata, which is (almost) identical to this problem.

Here's my solution:

const rangeify = a => {
  const res = [];
  let run = []
  
  for (let i = 0; i < a.length; i++) {
    run.push(a[i]);

    if (i + 1 >= a.length || a[i+1] - a[i] > 1) {
      res.push(
        run.length > 1 ? `${run[0]}-${run.pop()}` : run
      );
      run = [];
    }
  }
  
  return res.join(", ");
};

[
  [1,2,3,4,7,8,9,13,16,17],
  [],
  [1],
  [1, 2],
  [1, 3],
  [1, 2, 3, 8],
  [1, 3, 4, 8],
  [1, 1, 1, 1, 2, 3, 4, 5, 5, 16],
  [-9, -8, -7, -3, -1, 0, 1, 2, 42]
].forEach(test => console.log(rangeify(test)));

Solution 2:

This should work:

var array = [1, 2, 3, 4, 7, 8, 9, 13, 16, 17];

var ranges = [];
var index = 0;
while (index < array.length) {
    var rangeStartIndex = index;
    while (array[index + 1] === array[index] + 1) {
        // continue until the range ends
        index++;
    }

    if (rangeStartIndex === index) {
        ranges.push(array[index]);
    } else {
        ranges.push(array[rangeStartIndex] + " - " + array[index]);
    }
    index++;
}

console.log(ranges.join(", "));

Post a Comment for "How Do I Merge Consecutive Numbers In A Sorted List Of Numbers?"