If you follow (pray behind) the Imam you won't have to repeat a prayer, as he is responsible as the person who leads the prayer!
And normally people used to repeat a prayer they've done alone with a jama'a (as no one knows which one will be accepted) as mentioned in the sunan Books (except Ibn Majah)
Jabir bin Yazid bin Al-Aswad Al Amir told us that his father said:
"I
attended Fajr prayer with the Messenger of Allah (ﷺ)in Masjid Al
Khaif. When he finished praying, he saw two men at the back of the
people who had not prayed with him.
He said: 'Bring them here.' So
they were brought to him, trembling. He said: 'What kept you from
praying with us? They said: '0 Messenger of Allah (ﷺ) we have already
prayed in our lodgings.'
He said: 'Do not do that. If you have already
prayed in your lodgings, then you come to a Masjid in which there is a
congregation, then pray with them, and it will be a voluntary prayer
for you."'
(From Sunan a-Nasa'i, Jami' at-Tirmidhi)
However if you are confused there's a fiqh rule saying "من شك في شيء بنى على اليقين" this may mean: If you are confused about something you've done you should only accept (build on) what you are sure about.
Therefore it could be ok to repeat your prayer in that case especially if you did it on your own!
References and further Information:
If you can read Arabic here are some fatwas and some suggestions i found on the matter or parts of it maybe they are helpful:
1 (islamway),
2 (islamweb),
3 (islamweb),
4 (from a psychological PoV).
And here some English links for fatwas about having doubts about the validity of a prayer and repeating it:
1 (islamweb),
2 (islamqa)
A great overview (it seems from shi'a point of view), i guess it could be very helpful!
And finaly an other interesting post with a psychological point of view!