simplyfing common data values (parts of a string) in an array – by merging same attributes (data values)


I have arrays comprised of string elements like the following 2 examples. Each of the following lines is a string element of the array.

example1:

aaa bbb cc1 dd1  aaa bbb cc1 dd2  aaa bbb cc2 dd1  aaa bbb cc2 dd2  aaa bbb cc3 dd1  aaa bbb cc3 dd2  

example2:

bbb rrr nnn ttt ooo eee ddd fff contr  bbb sss nnn ttt ppp eee contr  bbb sss nnn aaa ooo eee ddd fff contr  bbb rrr nnn ttt yyy eee ddd fff contr  

I want to simplify and remove "redundant" lines by merging in a single line duplicate attributes. So the results should be:

example1:

aaa bbb cc1 dd1/dd2 aaa bbb cc2 dd1/dd2 aaa bbb cc3 dd1/dd2 

example2:

bbb rrr nnn ttt ooo/yyy eee ddd fff contr  bbb sss nnn ttt ppp eee contr  bbb sss nnn aaa ooo eee ddd fff contr  

(the results are an array as well).

My current approach goes like this: remove the first column then compare all elements. if equal lines are found, merge them. remove second column, compare. It becomes though somewhat complicated, as not all lines have the same number of data values (separated by spaces).

I’m stuck here. Any help would be welcome.