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

356 Views Asked by At

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))).
2

There are 2 best solutions below

1
Wojtek Surowka On BEST ANSWER

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).
0
RichardC On

The ordsets module handles ordered lists as sets, using the same API as the sets module. https://erlang.org/doc/man/ordsets.html