1

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))).
2240
  • 1,547
  • 2
  • 12
  • 30
Manika Sharma
  • 113
  • 1
  • 12

2 Answers2

2

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

RichardC
  • 10,412
  • 1
  • 23
  • 24
2

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).
Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51