President Donald Trump has taken credit after congressman Mark Sanford lost his bid for re-election in a stunning primary defeat hours after the president injected himself into the bitter Republican race by stoking memories of the incumbent's extramarital affair several years ago.

In the most dramatic result in primaries across five states Tuesday, Sanford, a lawmaker from South Carolina, was the second incumbent House Republican to lose a primary this year - the latest victim of intense divisions among the GOP in the Trump era.

Though he has a generally conservative voting record, his criticism of Trump as unworthy and culturally intolerant made him a target of the president's most dedicated supporters, who often elevate loyalty over policy.

Sanford was defeated in his first congressional district contest by state Katie Arrington, who spent her campaign blasting Sanford as a 'Never Trumper.'

And hours before polls closed, Trump posted a startlingly personal attack on Twitter, calling Sanford 'very unhelpful.'

'He's MIA and nothing but trouble,' Trump continued. 'He is better off in Argentina.'

The swipe was a reference to Sanford's unexplained disappearance from the state in 2009, which he later said was part of an affair he was carrying on with a woman in Argentina.

President Trump endorsed Katie Arrington in her primary challenge to Rep. Mark Sanford, a vocal Trump critic

Mark Sanford faced a tough primary contest Tuesday in part because of his criticism of Trump

Trump gave his thoughts on the race just about three hours before polls closed

After Katie Arrington's victory in South Carolina, Trump again took to Twitter to gloat

With 98 percent of the votes counted prognosticators declared Sanford's opponent, state Rep. Katie Arrington, the congressional race's primary victor.

Arrington, who had characterized Sanford as a disloyal Never-Trumper, was ahead by four percent with 50.5 percent of the vote to Sanford's 46.5 percent.

After the results, Trump again took to Twitter to gloat.

'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. Congrats to Katie Arrington!' he wrote.

Trump had endorsed Arrington in at Tuesday tweet on the race as he flew from Singapore after his Kim Jong-Un summit to a refueling stop in Guam.

'She is tough on crime and will continue our fight to lower taxes. VOTE Katie!,' he wrote.

Trump also shamed Sanford for the extramarital affair that led to his fall from grace with national Republicans.

When Sanford was governor of South Carolina, he was revealed to be having an extramarital affair.

He initially claimed he was hiking the Appalachian Trail but was actually in Argentina with his lover, María Belén Chapur.

But Trump has also been accused of having extra martial affairs.

An alleged affair with porn actress Stormy Daniels and hush-money payoff just before the 2016 election have been a distraction during his presidency.

He divorced his first wife, Ivanka Trump, and married his mistress Marla Maples.

There were three candidates, including Sanford, in the Republican primary. If no one received a majority of votes, a runoff would have taken place in two weeks.

Arrington was projected early Wednesday morning to win outright.

The president has been picking and choosing winners and commenting on the Republican Party's showing in primaries all through the midterms.

'How could Jeff Flake, who is setting record low polling numbers in Arizona and was therefore humiliatingly forced out of his own Senate seat without even a fight (and who doesn't have a clue), think about running for office, even a lower one, again? Let's face it, he's a Flake!' ' the president said last week of a Trump critic and incumbent GOP senator.

Katie Arrington celebrates after casting her vote on Tuesday

Sanford became a national name when his extra martial affair was revealed when he said he was hiking Appalachian Trail but was actually in Argentina

President Trump took credit last week when a Republican candidate made it through California's top-two primary system to get a spot on the gubernatorial ballot

Sanford is a house hold name in South Carolina and had never lost a political race, until now. He carefully rebuilt his reputation after his affair, and won a seat in the House of Representatives.

During the 2016 campaign he famously said Trump should 'shut up' and stop lashing out at his critics. He also criticized Trump's actions while in office such as when he called the president's new tariffs on steel and aluminum 'an experiment with stupidity.'

'I'm not looking for disagreement with the administration,' Sanford told Politico. 'But it comes to, you cannot walk away from it if you're really listening to the people I spend so much time with here at home.'

Arrington aired several TV commercials pointing out Sanford's criticism of the president. She has focused her campaign on her support for Trump.

Primary voters tend to be the red-meat party loyalists - the kind of voter that put Trump into office.

'We need a seat at the table. Our president is going to be the president until 2020 at a minimum, or 2024, and Mark has ostracized him to the point where there will never be a seat at the table for him,' she said at a campaign event.

Arrington endorsed Senator Marco Rubio in the 2016 presidential contest and said she was following the lead of the state's governor at the time, Nikki Haley.

She said she fully supported Trump once he had the nomination.

Arrington has also referred to Sanford's affair.

'Bless his heart, but it's time for Mark Sanford to take a hike — for real this time,' Arrington said in one TV ad.