Efficient Representation of a Sparse Array in JavaScript
I’m using an array to represent ranges of numbers. For example, in this array,
1
[1, 2, 3, 4, 15, 16, 17, 18]
there are two sequences of consequitive numbers: 1-4 and 15-18. In order to save space when serializing this array, it can be represented as ranges of numbers. For example:
1
[ [1, 4], [15, 18] ]
This function takes in an array of numbers, and returns an array of ranges: