# Find partial "row" matches and deleting them

I have data in the following form

    data = {{A,Null,C,D},{A,B,Null,D},{A,Null,C,Null},{A,B,C,D}}


I want to identify entries 1-3 in that list and delete them, since they are already included in the 4th entry, which is the most complete one.

My first idea was to delete the Null entries and search if that shortened entry is included in another entry, but I can't get it to work.

• If {A,B,C,D} is not present in the list what do you expect as the output ? Feb 6 '20 at 10:31
• As per your suggestion: If[SubsetQ[Last@data,#],Sequence[],#]& /@ data /. Null -> Sequence[] works in this case, but as @Lotus pointed out, it is not clear what output do you expect when {A,B,C,D} is not there Feb 6 '20 at 10:33
• For {{A, Null,C, Null},{A, Null, C, D}} I'd like to retain {A, Null, C, D} Feb 6 '20 at 10:49

## 1 Answer

data = {{a, Null, c, d}, {a, b, Null, d}, {a, Null, c, Null}, {a, b, c, d}};

DeleteDuplicates[Reverse@SortBy[DeleteCases[#, Null] &] @ data,
SubsetQ @@ (DeleteCases[#, Null] & /@ {##}) &]


{{a, b, c, d}}

data2 = {{a, b, c, Null}, {a, Null, c, Null}, {a, b, c, Null}, {a, Null, c, d}};

DeleteDuplicates[Reverse@SortBy[DeleteCases[#, Null] &] @ data2,
SubsetQ @@ (DeleteCases[#, Null] & /@ {##}) &]


{{a, Null, c, d}, {a, b, c, Null}}

• This look fantastic already, thanks. Would it be possible wih this method to retain the structure of the list in cases like: {{a,b,c,Null},{a,Null,c,Null},{a,b,c,Null},{a,Null,c,d}}? This returns {{a,c,d},{a,b,c}}, I need {{a,c,Null,d},{a,b,c,Null}}. I guess I could look up every element in the original list and put it in the right column but maybe there is an easier way. Feb 6 '20 at 10:57
• @testrado, please see the updated version.
– kglr
Feb 6 '20 at 11:06
• It took me a while to completely understand that line of code, but I just ran it on the data, it seems that it worked perfectly. Many thanks! Feb 6 '20 at 21:30