Related Tags

union
set
julia
communitycreator

# How to use the union() method of set in Julia

Programming Bytes

### Overview

The union method finds the unionReturns a new set of elements having all distinct elements of the given sets. of the given sets.

### Syntax

union(set1, set2)


### Arguments

This method takes two sets as an argument.

### Return value

This method returns a set containing all the elements of the given sets.

### Code

The code below demonstrates the use of the union method:

# union of two set
println(union(Set([1, 2]), Set([1, 4])))

# union of two set
println(union(Set([1, 2]), Set([3, 4])))

# union of two set
println(union(Set([1, 2]), Set([1, 2])))
An example of using the union method

### Explanation

Line 2: We use the union method to get a set containing all the sets’ elements— [1,2] and [1,4]. The union method returns [4,2,1].

Line 5: We use the union method to get a set containing all the sets’ elements— [1,2] and [3,4]. The union method returns [4,2,3,1].

Line 8: We use the union method to get a set containing all the sets’ elements— [1,2] and [2,1]. The union method returns [2,1].

RELATED TAGS

union
set
julia
communitycreator

CONTRIBUTOR

Programming Bytes
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time