If you are not married, you should get married. And if you are married, you should, with guts and determination, keep aloof from temptations or things luring to commit a sin. Also, think of the punishment of Allah Almighty when you feel doing a sin. On the other hand, you should do ba?iah with a righteous person follower of Sunnah and abide by his instructions. Also, you can swear that if you commit a sin you will keep fast of a month continuously, perform one hundred rakah nafl salah or sadaqah of you half income, thus the tendency to the sin shall be weakened.
Allah (Subhana Wa Ta’ala) knows Best
Darul Uloom Deoband