Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you think you have some skills to help others

help someone and become a part of our community - List Q & A

JavaScript - run-length encoding example

0 contributions
4 points

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
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more