/* Unique characters in each string in Picat. http://rosettacode.org/wiki/Unique_characters_in_each_string """ Task Given a list of strings, find the characters appearing exactly once in each string. The result should be in alphabetical order. Use the following list for this task: ["1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"] For this list, the result would be: 1 2 3 a b c """ This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import ordset. main => go. go => L = ["1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"], U = [], foreach(S in L) U := U ++ [new_ordset([C : C = 1 in counts(S)])] end, println(fold(intersection,U.first,U.tail)), nl. go2 => L = ["1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"], U = new_ordset([]), foreach(S in L) SO = new_ordset([C : C=1 in counts(S)]), if U == [] then U := SO else U := intersection(U,SO) end end, println(U), nl. go3 => L = ["1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"], U = [new_ordset([C : C = 1 in counts(S)]) : S in L], println(fold(intersection,U.first,U.tail)), nl. % Return a map of occurrences of each element in the list L counts(L) = Map => Map = new_map(), foreach(E in L) Map.put(E,Map.get(E,0)+1) end.