After duplicating an activity in a course, some groups in that course are duplicated.
Investigation showed that this is basically happening because the teacher who сreated the groups which are duplicating, presumably did not type the descriptions in Moodle, but copypasted them from an office app, such as MS Word.
When duplicating activity, Moodle actually backs it up, including groups, and then restores it as a new copy. Each group from backup is checked if it already exists in the course and if not, it's (re-)created. To do the check, Moodle searches for groups which names AND descriptions exactly match those in backup. See restore_stepslib.php, function process_group().
If group description is pasted from MS Word, it may contain character sequences 0D 0A (\r\n) which are sanitized somewhere on backup being converted to a single byte character 0A (\n), hence group description in backup will not strictly match that in the database and the group will duplicate.
Attaching 2 files:
1. desc-db.txt — description of a group containing special characters, retrieved from the database.
2. groups.xml — groups file from course backup.