How to remove duplicated node groups?
It is indeed annoying when you append materials from your library file and Blender automatically creates duplicates for all node groups with the same name, even when the groups are supposed to be exactly the same.

I have created a script that eliminates these duplicates efficiently for my usage. It searches all existing node groups, materials, worlds and if finds any duplicates (ending with .001, .002, etc), it replaces them with the original group if it is found.

After running the script the eliminated node groups will have 0 users, so after you save and reopen your file, they should disappear and your group list should be clean :)

import bpy

def main():
    print("\nEliminate Node Group Duplicates:")

    #--- Search for duplicates in actual node groups
    node_groups = bpy.data.node_groups

    for group in node_groups:
        for node in group.nodes:
            if node.type == 'GROUP':
                eliminate(node)

    #--- Search for duplicates in materials
    mats = list(bpy.data.materials)
    worlds = list(bpy.data.worlds)

    for mat in mats + worlds:
        if mat.use_nodes:
            for node in mat.node_tree.nodes:
                if node.type == 'GROUP':
                    eliminate(node)

#--- Eliminate the node group duplicate with the original group if found
def eliminate(node):
    node_groups = bpy.data.node_groups

    # Get the node group name as 3-tuple (base, separator, extension)
    (base, sep, ext) = node.node_tree.name.rpartition('.')

    # Replace the numeric duplicate
    if ext.isnumeric():
        if base in node_groups:
            print("  Replace '%s' with '%s'" % (node.node_tree.name, base))
            node.node_tree.use_fake_user = False
            node.node_tree = node_groups.get(base)

#--- Execute main
main()


A variant to eliminate OSL scripts

I never used OSL scripts, but as @IPv6 have asked, I guess this should work to eliminate duplicates of OSL scripts:

import bpy

print("\nEliminate OSL Scripts:")
texts = bpy.data.texts

for mat in bpy.data.materials:
    if mat.use_nodes:
        for node in mat.node_tree.nodes:
            if node.type == 'SCRIPT':
                name = node.script.name
                (base, sep, ext) = name.rpartition('.')

                # Replace the numbered duplicate with the original if found
                if ext.isnumeric():
                    if base in texts:
                        print("  Replace '%s' with '%s'" % (name, base))

                        # Replace the script duplicate for this node
                        node.script = texts.get(base)

                        # Remove the script duplicate from bpy.data.texts ?
                        texts.get(name).user_clear()