The development of signal processing and coding techniques for magnetic recording channels has taken place in an environment of escalating demand for higher data transfer rates and storage density. The error probability of many magnetic-recording systems may be characterized in terms of the differences between the sequences that may be recorded. In fact, the bit-error rate is often dominated by a small set of potential difference patterns. Binary codes have been proposed in the literature which exploits this fact. The codes designed to avoid the difference patterns by constraining the set of allowed recorded sequences, have been shown to improve system performance. We propose here a new algorithm for determining a code that avoids the specified difference patterns.