listseterlangerlang-shellset-operations

Is there anything to perform union of two lists in Erlang?


I found out that there are set operations for sets in Erlang, but I could not find out similar operations for lists. I want to perform the basic union operation in lists in Erlang:

A = [1, 2, 3]
B = [1, 2, 5]
C = A union B = [1, 2, 3, 5]

How can I perform this operation in Erlang?

I did the following using sets, though, and it works. I was just wondering, if I can do this without sets.

C = sets:to_list(sets:union(sets:from_list(A),sets:from_list(B))).

Solution

  • You can concatenate the two lists and then sort them, removing duplicates:

    A = [1, 2, 3],
    B = [1, 2, 5],
    C = lists:usort(A ++ B).