Home > Net >  Removing duplicates in lists within a list
Removing duplicates in lists within a list

Time:03-31

How would I go about removing duplicate elements of a list that is itself within a list? For example [[x,x],[x,y],[y,y,z]] should be returned as [[x],[x,y],[y,z]].

CodePudding user response:

lists_dedupe(Ls, Ds) :-
    maplist(list_to_set, Ls, Ds).

e.g. your example:

?- lists_dedupe([[x,x],[x,y],[y,y,z]], R).
R = [[x], [x, y], [y, z]]

Sets are basically lists without duplicates, so this maps 'convert to set' over the input to get the output.

  • Related