How to correctly undo data exchange between arrays?

That particular approach is prone to issues because “before” is relative to each user. Two concurrent insertions of the same element will result in both elements being deleted.

However, yes, you can clean up the document from duplicates. But you need to decide on a measurement for priority that doesn’t depend on time or before-relations (again, these things are relative). For example, you could always retain the first occurrence (as in position from left to right) of an element and delete the rest.