Open In App

Getting union of sets in Julia – union() and union!() Methods

Improve
Improve
Like Article
Like
Save
Share
Report

The union() is an inbuilt function in julia which is used to construct the union of the specified sets.

Syntax: union(s, itrs…)

Parameters:

  • s: Specified first set.
  • itrs: Specified second set.

Returns: It returns the constructed union of the specified sets.

Example:




# Julia program to illustrate 
# the use of union() method
   
# Getting the constructed union of the specified sets.
println(union([5, 10], [15, 20]))
println(union([2, 4], [2, 4, 6]))
println(union([1, 3], 5:7))
println(union(Set([2, 4, 6]), 1:5))


Output:

[5, 10, 15, 20]
[2, 4, 6]
[1, 3, 5, 6, 7]
[4, 2, 6, 1, 3, 5]

union!()

The union!() is an inbuilt function in julia which is used to construct the union of passed in sets and overwrite s with the result.

Syntax:
union!(s::Union{AbstractSet, AbstractVector}, itrs…)

Parameters:

  • s: Specified set.
  • itrs: Specified iterator.

Returns: It returns the constructed union of passed in sets and overwrite s with the result.

Example:




# Julia program to illustrate 
# the use of union !() method
   
# Getting constructed union of passed 
# in sets and overwrite s with the result.
s1 = Set([1, 2, 3, 4]);
union !(s1, 5:7:9);
println(s1)
  
s2 = Set([1, 2, 3, 4]);
union !(s2, 1:2:3);
println(s2)


Output:

Set([4, 2, 3, 5, 1])
Set([4, 2, 3, 1])


Last Updated : 26 Mar, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads