Hours after a last-minute broadside from President Donald Trump, South Carolina Republican Rep. Mark Sanford lost his primary race on Tuesday night, possibly ushering in an end to a political career plagued by a major scandal.

With 99 percent of the state's precincts reporting, state Rep. Katie Arrington had 50.5 percent with 32,441 votes to Sanford's 46.5 percent with 29,899 votes. Arrington needed over 50 percent of the vote to avoid a runoff.

Earlier Tuesday night, as the results were still coming in, Sanford suggested that he was ready to throw in the towel. "I think I'll end up losing this election," he said.

Moments after arriving back in the U.S. from Singapore on Wednesday morning, Trump tweeted his congratulations.

"My political representatives didn’t want me to get involved in the Mark Sanford primary thinking that Sanford would easily win - but with a few hours left I felt that Katie was such a good candidate, and Sanford was so bad, I had to give it a shot," the president said. "Congrats to Katie Arrington!"

Arrington, a political newcomer who has held her seat since 2016, spent the campaign blasting Sanford — who had never lost an election — over his lack of loyalty to Trump and attacked him in ads for a much-publicized extramarital affair with an Argentinian journalist.

Trump hit Sanford for the affair, too.

"He is better off in Argentina," Trump tweeted, alluding to Sanford's relationship with the Argentinian journalist, which burst into the headlines in 2009 when he was South Carolina's governor.

"Mark Sanford has been very unhelpful to me in my campaign to MAGA. He is MIA and nothing but trouble," Trump wrote Tuesday afternoon, less than three hours before polls closed. The "MIA" mention was apparently a reference to the then-married Sanford's disappearance from his state for days during the affair when he secretly traveled to Argentina to be with his lover.

Trump also threw his support behind Arrington.

"She is tough on crime and will continue our fight to lower taxes. VOTE Katie!" the president tweeted Tuesday while en route back to the White House from his Singapore nuclear summit with North Korean leader Kim Jong Un.

Arrington returned the favor, telling the crowd at her election night party, "We are the party of Donald J. Trump," according to The Post and Courier in Charleston.

Prominent Republican members of Congress, including House Speaker Paul Ryan, R-Wis., came to Sanford's defense after Trump's attack on him. A source on Ryan's political team told NBC News that the speaker, in keeping with his policy of supporting all GOP incumbents for re-election, backs Sanford.

Sanford had been running an ad buy in recent days that hit back at Arrington's criticism that he hasn't been sufficiently supportive of the president, who won the congressional district by double digits in 2016.

Sanford had previously called the president's steel and aluminum tariffs "an experiment with stupidity," and he suggested that Trump's rhetoric has been divisive and bad for the country.

Only four Republican members of Congress have voted for Trump-backed legislation less often than Sanford, according to an analysis of voting data by FiveThirtyEight. And during the 2016 race, Sanford, who supported Trump, nevertheless called on the GOP nominee to release his tax returns.

In one of his TV commercials, Sanford told viewers, "Overwhelmingly, I've voted with the president, and a long list of independent scorekeepers will tell you so." In another, he voiced support for Trump’s signature immigration proposal, saying, "I've supported efforts to secure the border and build a wall."

Arrington will go up against lawyer Joe Cunningham, who won the Democratic primary in the district. A Democrat has not won South Carolina's 1st Congressional District since 1978.

South Carolina residents also voted in gubernatorial primaries for both parties. On the GOP side, they sent incumbent Gov. Henry McMaster to a June 26 runoff along with businessman John Warren, after no candidate passed the 50 percent threshold. On the Democratic side, voters picked state Rep. James Smith to advance to the November general election.

Voters in Maine, North Dakota, Nevada and Virginia also headed to the polls Tuesday for primaries in congressional, Senate and gubernatorial races.

In Virginia, Corey Stewart, a Trump-supporting immigration hardliner who campaigned on keeping up Confederate monuments, narrowly won the GOP U.S. Senate primary, and will go up against incumbent Democratic Sen. Tim Kaine this fall.

With 100 percent of Virginia precincts in, Stewart, the chairman of the Prince William County Board of Supervisors, had 44.9 percent of the vote, compared to state Delegate Nick Freitas, a former Green Beret who'd received backing from a key conservative group, with 43.1 percent.

Stewart's victory could be a gift for Kaine in blue-leaning Virginia, where a Republican hasn't won a statewide race since 2009. Stewart campaigned as an ardent supporter of the Second Amendment and even revived false "birther" allegations that President Barack Obama wasn't born in the U.S.

But voters in Virginia, who in 2016 picked Hillary Clinton — with whom Kaine ran as the Democratic vice presidential nominee — over Trump, and who in 2017 chose Democrat Ralph Northam over Republican Ed Gillespie in the gubernatorial election, have consistently rejected conservative Republicans in statewide races in recent years.

Virginia GOP Senate nominee Corey Stewart speaks during a news conference at the Capitol in Richmond, Virginia, on Feb. 22, 2018. Steve Helber / AP file

In Virginia, voters also picked their Democratic challengers in several congressional districts, including in the state's 10th Congressional District, where state Sen. Jennifer Wexton, the Democratic Party favorite, advanced to take on incumbent GOP Rep. Barbara Comstock, one of the most vulnerable Republicans in Congress, according to The Associated Press.

The results in Virginia marked a strong showing for female candidates.

In addition to Wexton’s win in the 10th Congressional District Tuesday night, Elaine Luria won in the 2nd Congressional District, Abigail Spanberger won in the 7th Congressional District, while Leslie Cockburn won the 5th Congressional District Democratic nomination at a nominating convention last month.

Meanwhile, in Nevada, voters sent incumbent Republican Sen. Dean Heller and Democratic Rep. Jacky Rosen to face off in the general election in a contest that could help decide control of the U.S. Senate, according to an Associated Press projection.

Nevada voters also advanced GOP Attorney General Adam Laxalt and Democratic Clark County Commission Chair Steve Sisolakto the gubernatorial general election, the AP projected.

In Maine, voters headed to the polls to choose candidates from both major parties in primary races for the state's gubernatorial race.

Maine is using ranked-choice balloting to determine who wins. Under the system, in primaries with more than two candidates, voters will rank candidates in order of preference on their ballots. If no candidate receives a majority of first-choice votes, the candidate with the fewest number of first-choice votes is eliminated. Ballots cast for the eliminated candidate are then recast for the voter's second-choice pick. The elimination and retabulation process continues until there are two candidates left in the race; the one with the most votes is declared the winner.

While Maine's Republican gubernatorial primary was decided on election night when GOP candidate Shawn Moody won a majority according to the AP, the Democratic gubernatorial primary will be decided through the ranked-choice voting system within a week, after ballots are transported to the state capitol for the retabulation process.