Leaving only doubled values in GENRE tag field

I have this perfectly working awesome code, which I was given here a couple of months ago, that I use to maintain order and clarity in my GENRE tag

Replace "GENRE": "##" -> |

Replace "GENRE": " " -> ""

Replace "GENRE": "|" -> "|0"

Guess values %GENRE%: |%GENRE%

Format value "GENRE_ORDER": '000|001|002|003|004|005|006|007|008|009|010|011|012|013|014|015|016|017|018|

Format value "GENRE": $regexp($regexp($regexp(%GENRE_ORDER%,$regexp(%GENRE_ORDER%,'(?>\b('%GENRE%')\b)',),),'\|+','|'),'\|$',)

Replace "GENRE": "|" -> "  ##"

But in time, a new need arose for a code working something like in reverse

Right now a case like this

##50  ##01  ##99  ##99  ##01  ##01

is treated by it to look like that

##01  ##50  ##99

but I would need it to become like this

##01  ##99

So right now the code takes out all doubled [tripled etc.] values that start with the marker >>##<<, leaving just one instance of them; and also rearranges everything what is left in the order listed within it

And the alternative version should only leave all doubled [tripled etc.] values that start with a marker >>##<<, leaving just one instance of them, while at the same time cutting out all not repeated [single appearing] values; and also rearrange them in the given order

The purpose for this is: if I have a "Track 1" with some values and "Track 2" with some values, and then combine them into single file "Track 1 + Track 2" I have to manually look at values from both original files and copy / put only the common ones into the new one. And that takes a lot of time and it is very easy to miss something or even make a mistake while doing so, because one file can have even 20-25 values

An I can't just select and copy values from "Track 1" and put them to "Track 1 + Track 2" and then select and copy values from "Track 2" and also put them to "Track 1 + Track 2" and finally run the old code, because for example "Track 1" could have >>##01<< while "Track 2" could have not; which in turn would falsely leave me with >>##01<< in "Track 1 + Track 2", while in a mater of fact it should only be in "Track 1"

The principles of the set theory ... and how it can be handled by $regexp function ...
have already been rolled out.
Now again with a new example ... how to get only the common elements out of two sets.

Set1 = 1, 4, 7, 10
Set2 = 2, 4, 6, 10 ,12

Set1 without Set2 is ... Set1 - Set2 ==> 1, 7
Set2 without Set1 is ... Set2 - Set1 ==> 2, 6, 12

Both results united together is ....
Set3 = (Set1 - Set2) + (Set2 - Set1) ==> 1, 2, 6, 7, 12

The target set is ...
Set4 = (Set1 + Set2) - Set3 ==> 4, 4, 10, 10

Set4 can be reduced to unique common values ...
Set4 ==> 4, 10



This example is very clear

But understanding this simple math issue and translating it into a quite complex code are two completely different levels of computer skills; which I obviously lack

After using my system for quite some time now, I have come to conclusion that such an automatic process would also generate some inconsistencies

Every now and then I would need to stop and think for myself. A proper code would speed things up but would not make it 100% correct everytime