Editor classes shared mateials

I am trying to remove materials that get imported twice by mistake in the Unity editor using editor scripting.
To check duplicates I get a selection of prefabs and from the renderers attached to them I access the materials.
If I find that two materials have the same name but a different location, say x and y then I set the material x to the sharedMaterial property of y and delete the material y using its asset location. The naming convention followed ensures that two different materials do not have the same name.
But the problem I face currently is that if I try to assign a material to an element of the sharedMaterials
array then the assignment does not take place.
I will need this since most renderers have more than one material.
Any pointers will be of great help.
Thanks
Anur

I believe it works the same way as, for instance, changing vertices of a mesh: you have to create a new array and assign it to sharedMaterials, rather than modifying individual elements.