A Beautiful Teaching of the Baal Shem Tov zt"l:
So, the Baal Shem Tov explains beautifully, that, the reason for this is because Hashem lets you see the person doing that certain transgression for a reason - likely for you to realize that you yourself have that same flaw, and to then correct it.
The Baal Shem Tov himself followed this teaching, as can be seen from the following story: He once saw someone violating Shabbos, and so, following his teaching, he looked into himself for that flaw. After looking very deep and not finding a violation of Shabbos within himself, he Davened (prayed) that his transgression be revealed to him. And his prayers were answered! It was revealed to him that once, he had heard somebody insulting a Tzaddik (righteous person) and he had not rebuked the person. This was like a violation of Shabbos, as the Zohar says that a Tzaddik has the Kedusha (holiness) of Shabbos.1 And so, he likely resolved to fix that flaw.