Languages
[Edit]
EN

JavaScript - run-length encoding example

4 points
Created by:
Marcin
15680

Run-length encoding (RLE) is lossless data compression method. It is widely used to compress data where some elements often are repeated in sequence. 

1. Main compression concept

Input data:

AAAAAABBBBBBACCCCCCCCCCCCCDDDAAAAAAAAAAAAA

Compressed data:

6A6B1A13C3D13A

2. Letters compression example

// ONLINE-RUNNER:browser;

function compressText(text) {
    var result = '';

    if (text.length > 0) {
        var count = 1;
        var value = text[ 0 ];

        for (var i = 1; i < text.length; ++i) {
			var entry = text[ i ];
          
            if (entry == value) {
                count += 1;
            } else {
              	result += count + '' + value;
              
              	count = 1;
				value = entry;
            }
        }
      
      	result += count + '' + entry;
    }

    return result;
}

// Example:

var text = 'AAAAAABBBBBBACCCCCCCCCCCCCDDDAAAAAAAAAAAAA';
var compressedText = compressText(text);

console.log(compressedText); // 6A6B1A13C3D13A

3. Coma separated compression example

// ONLINE-RUNNER:browser;

function compressText(text) {
    var result = '';

    if (text.length > 0) {
        var count = 1;
        var value = text[ 0 ];

        for (var i = 1; i < text.length; ++i) {
			var entry = text[ i ];
          
            if (entry == value) {
                count += 1;
            } else {
              	result += count + ':' + value + ',';
              
              	count = 1;
				value = entry;
            }
        }
      
      	result += count + ':' + entry;
    }

    return result;
}

// Example:

var text = 'AAAAAA55555ACCCCCCCCCCCCC111AAAAAAAAAAAAA';
var compressedText = compressText(text);

console.log(compressedText); // 6:A,5:5,1:A,13:C,3:1,13:A

4. Array compression example

// ONLINE-RUNNER:browser;

function compressArray(array) {
    var result = [ ];

    if (array.length > 0) {
        var count = 1;
        var value = array[ 0 ];

        for (var i = 1; i < array.length; ++i) {
			var entry = array[ i ];
          
            if (entry == value) {
                count += 1;
            } else {
                result.push(count);
                result.push(value);
              
              	count = 1;
				value = entry;
            }
        }
      
        result.push(count);
        result.push(value);
    }

    return result;
}

// Example:

var array = [
    1, 1, 1, 1, 1, 1, // 3x 1
    6, 6, 6, 6, 6, // 5x 6
    1, // 1x 1
    3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 13x 3 
    8, 8, 8, // 3x 8
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // 13x 1
];
var compressedArray  = compressArray(array);

console.log(compressedArray ); // 6,1,5,6,1,1,14,3,3,8,13,1

References

  1. Run-length encoding - Wikipedia
Native Advertising
50 000 ad impressions - 449$
­čÜÇ
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

ÔŁĄ´ŞĆ­čĺ╗ ­čÖé

Join