I tried to find which settings to include, not delete. Something like that?
(1) List of elements and indexes of their sets
(2) Translate the list of answers with indexes of sets that have elements that appear only in them
(3) Combine the map from (1), and if the index of the set of elements is not listed in the answer list, add the index of the smallest number in which the element is located in the answer.
Haskell Code:
import Data.List (nub, minimumBy, intersect) sets = [["a","b","c","e"],["a","b","d"],["a","c","e"]] lengths = map length sets --List elements and the indexes of sets they are in mapped = foldr map [] (nub . concat $ sets) where map ab = comb (a,[]) sets 0 : b comb result [] _ = result comb (a,list) (x:xs) index | elem ax = comb (a,index:list) xs (index + 1) | otherwise = comb (a,list) xs (index + 1) --List indexes of sets that have elements that appear only in them haveUnique = map (head . snd) . filter (\(element,list) -> null . drop 1 $ list) $ mapped --Comb the map and if an element set-index is not in the answer list, --add to the answer the index of the smallest set that element is in. answer = foldr comb haveUnique mapped where comb (a,list) b | not . null . intersect list $ b = b | otherwise = minimumBy (\setIndexA setIndexB -> compare (lengths!!setIndexA) (lengths!!setIndexB)) list : b
OUTPUT:
*Main> sets [["a","b","c","e"],["a","b","d"],["a","c","e"]] *Main> mapped [("a",[2,1,0]),("b",[1,0]),("c",[2,0]),("e",[2,0]),("d",[1])] *Main> haveUnique [1] *Main> answer [2,1]