Ibn al Qayyim al Jawziyyah (rahimahullah) mentions in regards to unlawful love before marriage (i.e. haram sexual relations, or love for someone who you are unable to marry).
“And the cure for this deadly illness (i.e. unlawful love before marriage) is for the person that is afflicted to realise that this love is only due to his/her own delusions and ignorance.
So upon such a person is to first and foremost strengthen their Tawheed and reliance upon Allah, and secondly to increase in worship and busy themselves with it, so much so that they do not have any spare time letting their minds wander and think about their beloved.