Charity on behalf of the deceased is valid on the part of a relative or someone else
Question

Is it valid for someone to build a mosque or do any act of charity on behalf of a deceased person when he is not a family member or son of the deceased; rather he is one of his friends and acquaintances, who does that with the intention that the reward of his charity should go to that person. Is offering supplication for the deceased better than giving charity on his behalf? If possible, please give us evidence.
Praise be to Allah.Firstly:
In order for charity on behalf of the deceased to be valid, it is not stipulated that it be done by the relatives or children of the deceased; rather it is permissible for such charity to be given by one who is not related to him, just as it is valid for it to be given by a relative.
Ibn Qudaamah (may Allah have mercy on him) said: Whatever act of worship he does, and dedicates its reward to the deceased Muslim, it will benefit him, if Allah wills". (Al-Mughni  2/226).
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) said: It is permissible for a person to do an act of worship and obedience to Allah, may He be glorified and exalted, with the intention that it be on behalf of a deceased Muslim, whether that deceased person is one of his relatives or is one to whom he is not related". (Fataawa Noor ‘ala ad-Darb, by Ibn ‘Uthaymeen).
Based on that, it is valid for a person to give charity on behalf of his deceased friend, or to build a mosque with the intention that the reward go to him. All of these are things for which the reward will reach the deceased, if Allah wills.
Secondly:
Offering supplication (du‘aa’) for the deceased and giving charity on his behalf are all supported by religious texts which indicate that they are permissible, and there is no contradiction between the reports. So charity may be given on behalf of the deceased person whilst offering supplication for him at the same time. But in terms of which is better, some of the scholars are of the view that offering supplication for the deceased is better than giving charity on his behalf, or that it is the best thing that can be given to the deceased in all cases.
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) said: The best is to offer supplication for him, instead of giving charity on his behalf, or offering a sacrifice on his behalf, or doing Hajj on his behalf, because offering supplication for the deceased is what the Prophet (blessings and peace of Allah be upon him) taught us to do. It is proven from him that he said: “When a person dies, all his good deeds come to an end except three: ongoing charity, or beneficial knowledge, or a righteous child who will offer supplication for him.” Thus he mentioned the righteous child who offers supplication for him, and he did not say, or a righteous child who will offer charity on his behalf, or do Hajj on his behalf, and other righteous deeds, even though the hadith speaks of actions. As the Prophet (blessings and peace of Allah be upon him) did not mention doing things on behalf of the deceased, rather he mentioned offering supplication for him, it is known that offering supplication is the preferred option, and it is the best option. Hence I advise my Muslim brothers to strive to offer supplication for their deceased loved ones, instead of dedicating the reward of righteous deeds to them, and they should keep the reward of the righteous deeds for themselves, because the one who is still alive needs righteous deeds, and there is no one who dies except he dies with regrets: if he was a doer of good, he regrets not doing more, and if he was a doer of evil, he regrets that he did not repent. Allah, may He be exalted, says (interpretation of the meaning):
{until, when death comes to one of them, he says, “My Lord, send me back, 
That I might do righteousness in that which I left behind”} [al-Mu’minoon 23:99-100]
{And spend [in the way of Allah] from what We have provided you before death approaches one of you and he says, “My Lord, if only You would delay me for a brief term so I would give charity and be among the righteous.” 
But never will Allah delay a soul when its time has come. And Allah is Acquainted with what you do} [al-Munaafiqoon 63:10-11].
Whilst you are still living, you need righteous deeds, so do good deeds for your own sake, and offer supplication for your deceased loved ones, mothers and fathers, brothers and sisters, and others among the Muslims.
This is what is indicated by the Sunnah of the Prophet (blessings and peace of Allah be upon him).
Nevertheless, if someone gives charity on behalf of one who has died, or fasts on his behalf, or prays, intending that the reward should go to the deceased, there is nothing wrong with that, if he dedicates the reward of that to him". (Fataawa Noor ‘ala ad-Darb by Ibn ‘Uthaymeen).
And Allah knows best.