Sheriff Jim McDonnell made a dramatic gain Friday in his bid for re-election, with updated vote tallies showing him trailing challenger Alex Villanueva by only 335 votes, down from 4,927 on Wednesday.

According to the latest figures from Tuesday’s election, Villanueva, a retired sheriff’s lieutenant, has 867,964 votes, or 50.01 percent, compared to 867,629, or 49.99 percent, for McDonnell, who was chief of the Long Beach Police Department until he was elected sheriff in 2014.

The figures mark a significant gain for McDonnell, who is in danger of becoming the first Los Angeles County sheriff to lose a re-election bid in more than a century.

The vote-counting, however, is far from over. County election officials said Wednesday there were about 984,000 ballots that still need to be tallied. The updated figures released Friday reflect a count of about 100,000 of them.

McDonnell said Wednesday he was confident of emerging victorious, despite the deficit. Villanueva said Friday he also remains confident.

“While our lead will grow and I am confident that we are going to win, nothing is being taken for granted as we continue to watch the registrar count the remaining casted provisional and absentee ballots,” he said.