Huffman came up with this algorithm: you build a tree where each node's value is the sum of all child nodes, and each leaf contains an element to be compressed and a relative frequency. The tree is built by making a new node which is the parent to the two lowest-valued nodes. By traversing the tree, 0 for left and 1 for right, you get unique, optimal binary codes for each compressed element (the lower the frequency, the longer the code). There exist many variations as well.