This algorithm is performed on a Lewis structure (a diagram that shows all valence electrons). Oxidation state equals the charge of an atom after each of its heteronuclear bonds has been assigned to the more-electronegative partner of the bond (except when that partner is a reversibly bonded Lewis-acid ligand) and homonuclear bonds have been divided equally:
where each "—" represents an electron pair (either shared between two atoms or solely on one atom), and "OS" is the oxidation state as a numerical variable.
After the electrons have been assigned according to the vertical red lines on the formula, the total number of valence electrons that now "belong" to each atom is subtracted from the number  of valence electrons of the neutral atom (such as 5 for nitrogen in group 15) to yield that atom's oxidation state.
This example shows the importance of describing the bonding. Its summary formula, , corresponds to two structural isomers; the peroxynitrous acid in the above figure and the more stable nitric acid. With the formula , the simple approach without bonding considerations yields −2 for all three oxygens and +5 for nitrogen, which is correct for nitric acid. For the peroxynitrous acid, however, the two oxygens in the O–O bond each has OS = −1 and the nitrogen has OS = +3, which requires a structure to understand.
Organic compounds are treated in a similar manner; exemplified here on functional groups occurring in between methane () and carbon dioxide ():
Analogously for transition-metal compounds;  on the left has a total of 36 valence electrons (18 pairs to be distributed), and hexacarbonylchromium () on the right has 66 valence electrons (33 pairs):
A key step is drawing the Lewis structure of the molecule (neutral, cationic, anionic): atom symbols are arranged so that pairs of atoms can be joined by single two-electron bonds as in the molecule (a sort of "skeletal" structure), and the remaining valence electrons are distributed such that sp atoms obtain an octet (duet for hydrogen) with a priority that increases in proportion with electronegativity. In some cases, this leads to alternative formulae that differ in bond orders (the full set of which is called the resonance formulas). Consider the sulfate anion () with 32 valence electrons; 24 from oxygens, 6 from sulfur, 2 of the anion charge obtained from the implied cation). The bond orders to the terminal oxygens do not affect the oxidation state so long as the oxygens have octets. Already the skeletal structure, top left, yields the correct oxidation states, as does the Lewis structure, top right (one of the resonance formulas):
The bond-order formula at the bottom is closest to the reality of four equivalent oxygens each having a total bond order of 2. That total includes the bond of order  to the implied cation and follows the  rule requiring that the main-group atom's bond order equals  valence electrons of the neutral atom, enforced with a priority that proportionately increases with electronegativity.
This algorithm works equally for molecular cations composed of several atoms. An example is the ammonium cation of 8 valence electrons (5 from nitrogen, 4 from hydrogens, minus 1 electron for the cation's positive charge):
Drawing Lewis structures with electron pairs as dashes emphasizes the essential equivalence of bond pairs and lone pairs when counting electrons and moving bonds onto atoms. Structures drawn with electron dot pairs are of course identical in every way: