Frequent characters get short codes, rare ones get long codes
A typical character is stored using 8 bits (eight 0s or 1s). But if we give shorter codes to characters that appear often and longer codes to characters that appear rarely, we can reduce the total size. Enter a sentence and try compressing it yourself.
How it works: Repeatedly combine the two characters (or groups) with the lowest frequencies to build a tree. Once the tree is complete, label the left branch 0 and the right branch 1. The path from the root down to each character becomes that character's code.
Original size: 0 bitsCompressed size: 0 bitsCompression: 0%
💡 This method is actually used in compression for ZIP files, JPEG images, and MP3 music files. The more repeated patterns the data contains, the greater the compression effect.