Sets in Julia

31 Jul, 2021
Set in Julia is a collection of elements just like other collections like arrays, dictionaries, etc. Sets are different from arrays because sets are unordered collections of unique elements whereas the order of elements is strictly remembered in Arrays. Also, Arrays and dictionaries may contain duplicate values but Sets can’t. Each element in a set must be unique. Sets are mutable data types, which means its values can be altered, deleted, overwritten, etc. 
Sets can contain elements of any data type. Just like other collections, it is not mandatory in Julia to define the data type of a set before the declaration of the elements. Julia automatically analyzes the elements and defines the Set’s data type. Julia doesn’t allow to access elements of a set individually because it is an unordered collection and hence, there is no fixed index of any element. 
A set in Julia can be created with a pre-defined keyword Set(). This keyword accepts elements placed within square brackets([]) and creates a set by defining its data type based on the data type of the elements. 
Syntax: 
Set_name = Set([Value1, Value2, Value3, ....]) 
Julia
# Julia program to illustrate# the use of Sets # Creating an empty setSet1 = Set()println("Empty Set: ", Set1) # Creating a set with Integer valuesSet2 = Set([1, 2, 3, 4, 5, 2, 4, 6])println(Set2) # Creating a set with mixed datatypesSet3 = Set([1, 2, 3, "Hello", "Geeks"])println(Set3)
Output: 
Set items cannot be accessed by referring to an index value, since sets are unordered the items have no fixed index. But you can loop through the set items using a for loop, or ask if a specified value is present in a set, by using the in() keyword.
Julia
# Julia program to demonstrate# Accessing of elements in a set   # Creating a setSet1 = Set(["Geeks", "For", "Geeks"])println("\nInitial set:")println(Set1)   # Accessing element using# for loopprintln("\nElements of set: ")for i in Set1    println(i)end   # Checking the element# using in keywordprintln(in("For", Set1))
Output: 
Sets are mutable type collections in Julia, hence, their values can be modified with the use of certain pre-defined keywords. Julia allows adding new elements in a set with the use of ‘push!’ command. Adding elements to a set is quite similar to adding elements in an array, the only difference is, unlike arrays, elements in set can’t be added on a specific index because sets are unordered.  
Syntax: 
push!(Set_name, Value)
Python
# Julia program to demonstrate# Accessing of elements in a set    # Creating a setSet1 = Set(["Hello", "Geeks", 10])println("\nInitial set:")println(Set1)  # Adding an element to the setSet1 = push !(Set1, "Welcome")println("\nSet after adding one element:\n", Set1)  # Adding a range of elements# using for loopfor i in 1:5    push !(Set1, i)endprintln("\nSet after adding range of elements:\n", Set1)
Output: 
Julia allows merging elements of two sets with the use of ‘union’ keyword. Union operation takes both the sets as an argument and gives the result as another set having elements of both the sets. The actual values of both sets do not change unless the return value is stored in any of the two sets. There’s another variant of union function which is union!. Both the keywords perform similar operations, but the only difference is that the union! function will overwrite the existing set with the new merged set. 
Syntax: 
union(Set1, Set2)
or 
union!(Set1, Set2)
Example 1: Performing union operation 
Python
# Julia program to demonstrate# Accessing of elements in a set   # Creating a setSet1 = Set(["Hello", "Geeks", 10])println("\nSet1:")println(Set1) # Creating another setSet2 = Set([1, 2, 3, 4])println("\nSet2:")println(Set2) # Performing Merge using union operationprintln("\nMerged Set after union Operation:")println(union(Set1, Set2)) # Existing set after union operationprintln("\nSet1 after union operation:")println(Set1)
Output: 
Example 2: Performing union! operation  
Python3
# Sets taken from previous example # Performing Merge using union ! operationprintln("\nMerged Set after union ! Operation:")println(union !(Set1, Set2)) # Existing set after union ! operationprintln("\nSet1 after union ! operation:")println(Set1)
Output: 
As can be seen in the above code, union! operator after performing the merge operation updated the existing content of the Set1, but the union operator doesn’t make any changes to the existing sets and only performs the merge operation. 
Julia allows finding common elements of two sets with the use of ‘intersect’ keyword. Intersection operation takes both the sets as an argument and gives the result as another set having common elements of both the sets. The actual values of both sets do not change unless the return value is stored in any of the two sets. There’s another variant of intersect function which is intersect!. Both the keywords perform similar operations, but the only difference is that the intersect! function will overwrite the existing set with the new intersected set.
Syntax:  
intersect(Set1, Set2)
or 
intersect!(Set1, Set2)
Example 1: Performing intersect operation  
Python
# Julia program to demonstrate# Accessing of elements in a set   # Creating a setSet1 = Set(["Hello", "Geeks", 1, 2, 3])println("\nSet1:")println(Set1) # Creating another setSet2 = Set([1, 2, 3, 4, "Geeks"])println("\nSet2:")println(Set2) # Performing intersection using 'intersect' operationprintln("\nSet after intersect Operation:")println(intersect(Set1, Set2)) # Existing set after intersect operationprintln("\nSet1 after intersect operation:")println(Set1)
Output: 
Example 2: Performing intersect! operation 
Python3
# Performing intersection using 'intersect !' operationprintln("\nSet after intersect ! Operation:")println(intersect !(Set1, Set2)) # Existing set after intersect ! operationprintln("\nSet1 after intersect ! operation:")println(Set1)
Output: 
As can be seen in the above code, intersect! operator after performing the intersection updated the existing content of the Set1, but the intersect operator doesn’t make any changes to the existing sets and only performs the intersection. 
Julia allows finding different elements between two sets with the use of ‘setdiff’ keyword. This operator takes both the sets as an argument and gives the result as another set having different elements of both the sets. The actual values of both sets do not change unless the return value is stored in any of the two sets. There’s another variant of setdiff function which is setdiff!. Both the keywords perform similar operations, but the only difference is that the setdiff! function will overwrite the existing set with the new set.
Syntax: 
setdiff(Set1, Set2)
or 
setdiff!(Set1, Set2)
Example 1: Performing setdiff operation 
Python
# Julia program to demonstrate# Accessing of elements in a set   # Creating a setSet1 = Set(["Hello", "Geeks", 1, 2, 3])println("\nSet1:")println(Set1) # Creating another setSet2 = Set([1, 2, 3, 4, "Geeks"])println("\nSet2:")println(Set2) # Finding Difference using 'setdiff' operationprintln("\nSet after 'setdiff' Operation:")println(setdiff(Set1, Set2)) # Existing set after 'setdiff' operationprintln("\nSet1 after 'setdiff' operation:")println(Set1)
Output: 
  Example 2: Performing setdiff! operation 
Python3
# Finding Difference using 'setdiff !' operationprintln("\nSet after 'setdiff !' Operation:")println(setdiff !(Set1, Set2)) # Existing set after 'setdiff !' operationprintln("\nSet1 after 'setdiff !' operation:")println(Set1)
Output: 
As can be seen in the above code, setdiff! operator after performing the operation updated the existing content of the Set1, but the setdiff operator doesn’t make any changes to the existing sets and only gives the set of different elements.
Removing elements from a set is quite an easier task. Julia provides a pre-defined keyword ‘delete!’ to remove a specific element from a set. Just like the addition process, removal of element is not possible from any specific index because of its unordered nature. The exact element is to be passed to the delete! function as an argument in order to remove it from the set.
Syntax: 
delete!(Set_name, value)
Python
# Julia program to demonstrate# Accessing of elements in a set   # Creating a setSet1 = Set(["Hello", "Geeks", 1, 2, 3])println("\nInitial set:")println(Set1) # Removing an element from the setSet1 = delete !(Set1, "Hello")println("\nSet after deleting one element:\n", Set1) # Deleting whole setfor i in Set1    Set1 = delete !(Set1, i)end println("\nEmpty set after deleting all elements:\n", Set1)
Output: 
simranarora5sos
Julia-dataTypes
Julia
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.