Depends on details like time of day and environment (for what your options are; obviously the middle of a work meeting isn’t the right time to whip out a rosary and start counting beads).
For me I used to have a bigger problem with this than I do now, and the context/environment when the lustful thoughts came was when I lay down to sleep at night (because I had a years-long problem with insomnia dating back to childhood, and when you’re too exhausted to get up and walk around, but your brain refuses to sleep so you can’t do anything but think, for hours… those kinds of thoughts were a challenge for me at that time). And for real: Praying the rosary helped. My tip for this is to
really concentrate on every word of it, and on the mysteries, but especially on the words of the prayers. In my experience, I’d find my brain actually was too tired to stay awake if ‘praying’ was all I’d think about (maybe that was the enemy’s influence, where the enemy wanted me to be awake if I’d occupy myself with sin, but gave up and let me sleep if I persistently chose holiness), and I’d gradually be saying each Hail Mary slower and slower as I dropped off… and kept going until I was literally unconscious. Like:
“Hail Mary… full of… grace, the Lord. … … is… with… thee…”
My experience was that it’s literally impossible to simultaneously focus on all my attention on praying these words (even if my attention is fading as I fall unconscious, and it’s several seconds in between words) and to think lustful thoughts. Just keep redirecting your thoughts back to the words, if your imagination drifts. Focus stubbornly on the words. (For me, whereas I used to have insomnia that kept me awake for 3-4 hours every night, when I started praying the rosary and fully focusing on it, I’d find that I usually fell asleep before I could finish a whole rosary (five decades). Maybe twice or three times, I’ve still been awake and decided to ‘agere contra’ the enemy by going around again… and I’m pretty sure I fell asleep on the second go-round, in those cases.)
Beyond that context (which let’s be real, relies on you being in bed and able to fall asleep, so is not suitable for public places where you have to interact with other people), I’d stick to the advice suggested by Jason Evert, which is that whoever is the ‘object’ of the temptation that arises…
pray specifically for that person. Like, pray for them to have a good relationship with their family, to receive graces and blessings from God, whatever. Beyond also having a positive side effect for the other person (and you, in growing your holiness)… psychologically it seems to drain away lust, to force ourselves to think about the other person as a person (not object) by considering what is actually in their best interests and asking God for it.
Hopefully helpful? Keep at it. And definitely offer up all your attempts to God. This is a struggle that strengthens you.