Thomas Jefferson very nearly lost the presidential election in 1800 to Aaron Burr (who later conspired to establish an independent nation in Mexico and the Southwest and was tried for treason). Each candidate received seventy-three electoral votes (the only tie in American history). Members of the Electoral College could vote for two men and the Republicans planned to have several electors split their tickets so that Burr would come second in the voting but each thought others would do so and the resulting tie forced the election into the House of Representatives, where thirty-six ballots were required to break the deadlock.
Incredibly, the single vote which finally led to Jefferson's victory was illegitimate. Only later was it learned that the man who had cast the deciding vote -- William C.C. Claiborne (1775-1817) -- had been elected to Congress before he was of constitutional age.