Code similar to this is executed when a form is saved:
$wpdb->query("DELETE FROM Unit_list WHERE group = '$group_id'");
$order = 1;
$unique_units = array_unique($units);
foreach ($unique_units as $unit) {
$wpdb->insert("Unit_list", array("unit" => $unit, "group" => $group_id, "sort_order" => $order));
$order++;
}
A few times a day there would be some duplicates.
If the form was submitted twice with enough time for this part of the code to run each time there would be no duplicates because the second time would involve the whole first group being deleted.
For there to be duplicates this code must be running twice at the exact same time.
Double-clicking quickly would do it but if a third click was slow and not simultaneous the group would get deleted and not involve duplicates.
After working out that double clicking was the cause, the solution is to stop subsequent clicks of the form submission button from being detected.