What prevents you (among other things we needn't concern ourselves with here) from receiving Holy Communion is being in a state of mortal sin.
To be in a state of mortal sin, the sin needs first to be grave. That's met here.
Second you need to know it is a sin and know it is grave. That's met here.
But you also need to have full consent of the will. This means, among other things, that it has to be deliberate, and it has to be done with sufficient reflection. Transient consent doesn't count. Entertaining for a few seconds a sexual thought that intrudes on your consciousness would not meet this requirement.
If, on the other hand, a thought came in, and you did not dismiss it but seized on it, and fantasized on the thought extensively having the opportunity to reject it but refusing to do so, that would be another matter. Let's say you fantasized a sexual encounter from start to finish, stubbornly refusing graces to stop. That would likely be a mortal sin.
If your friend is struggling with unwanted, persistent sexual thoughts, he should regularly avail himself of the Sacrament of Reconciliation, which is very helpful. I also recommend an upcoming online deliverance prayer webinar coming up in two days hosted by:
If you are not able to participant in it, check back for other times when they may have similar webinars.
This program has delivered some very powerful, even miraculous fruits.