How do I make an addon from this code?
Maybe try this and see if it makes sense to you.
It really should also poll to see if objects are selected in the operator.

bl_info = {
"name": "Material Button Panel",
"author": "Your Name Here",
"version": (1, 0),
"blender": (2, 78, 0),
"location": "View3D &gt; Toolbar",
"description": "Button to Remove Duplicate MAterials",
"warning": "",
"wiki_url": "",
"category": "Material",
}


import bpy



class RemoveDupmatl(bpy.types.Operator):
    """Remove Duplicate Materials"""
    bl_idname = "object.remove_dupmatl"


    bl_label = "Remove Duplicate Materials"
    bl_options = { 'REGISTER', 'UNDO' }

    def execute(self, context):

        scene = context.scene


        #new code
        mat_list = bpy.data.materials

        for o in bpy.data.objects:
            for s in o.material_slots:
                if s.material.name[-3:].isnumeric():
                    # the last 3 characters are numbers
                    if s.material.name[:-4] in mat_list:
                        # there is a material without the numeric extension so use it
                        s.material = mat_list[s.material.name[:-4]]

        return {'FINISHED'}






class TestPanel(bpy.types.Panel):
    """A custom panel in the viewport toolbar"""
    bl_label = "Test Panel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
    bl_category = "Tests"

    def draw(self, context):
        layout = self.layout

        row = layout.row()

        row.label(text="Single Button")
        row = layout.row()
        row.operator("object.remove_dupmatl", text = "Remove Dup Material", icon = 'ROTACTIVE')








def register():
    bpy.utils.register_class(RemoveDupmatl)
    bpy.utils.register_class(TestPanel)


def unregister():
    bpy.utils.unregister_class(RemoveDupmatl)
    bpy.utils.unregister_class(TestPanel)




if __name__ == "__main__":
    register()