# F# Sets

**F# sets** are collection of items that can be arranged in an order without preserving the order in which the items are inserted. The collection of entries in a set cannot be duplicated.

Here, we will take a closer look at how it is possible to solve a variety of programming problems using the set data type in F# and how you can implement it in your program as well as the basics of creating and manipulating sets.

## Create Sets

There are several ways to create sets:

**Set.empty**can be used to create an empty set and add items to it using the add function.- Converting a sequence or list to a set.

Here are some techniques that are demonstrated in the following program:

#### Example:

And the output will be:

The set: set [1; 3; 7; 8] The list set: set [Aston Martin; Bentley; Bugatti; Chevrolet; Ford Mustang] The sequence set: set [2; 4; 6; 8; 10]

## Basic Operations on Sets

Below is a table showing the basic operations that can be performed on sets:

Value |
Overview |

add : ‘T → Set<‘T> → Set<‘T> | The function returns a new set to which an element has been added. An exception will not be raised if the given element is already part of the set. |

contains : ‘T → Set<‘T> → bool | The given element is evaluated to be true if it is part of the set given. |

count : Set<‘T> → int | In this method, you will find out how many elements are present in the set. |

difference : Set<‘T> → Set<‘T> → Set<‘T> | The method returns a new set of elements in which the elements of the second set have been removed from the first set. |

empty : Set<‘T> | A set that contains no elements of the specified type. |

exists : (‘T → bool) → Set<‘T> → bool | There is a predicate that tests whether any element in the collection satisfies the given criteria. A function, when input is predicate and input is a set of elements, then its objective is to compute the predicate i0, or …, or predicate iN. |

filter : (‘T → bool) → Set<‘T> → Set<‘T> | This function returns a new collection which consists of the elements of the collection which return true when a predicate is applied to them. |

fold : (‘State → ‘T → ‘State) → ‘State → Set<‘T> → ‘State | Take the elements of the set and apply the accumulating function to them all. |

foldBack : (‘T → ‘State → ‘State) → Set<‘T> → ‘State → ‘State | All elements of the set are accumulated according to the accumulating function given. |

forall : (‘T → bool) → Set<‘T> → bool | An element of the collection is tested to determine if it meets the predicate given in the collection. A function that takes p as input in which the elements are i0…iN, then computes p i0 && … && p iN. |

intersect : Set<‘T> → Set<‘T> → Set<‘T> | A method for computing the intersection between two sets is provided. |

intersectMany : seq<Set<‘T>> → Set<‘T> | This function calculates the intersection between a sequence of sets. It is required that the sequence is not empty. |

isEmpty : Set<‘T> → bool | Whether the set is empty or not is determined by this function. |

isProperSubset : Set<‘T> → Set<‘T> → bool | The value is true if at least one element of the second set is not in the first set, and each element of the first set is in the second set. |

isProperSuperset : Set<‘T> → Set<‘T> → bool | A true value is returned if each element of the second set is a part of the first set, and at least one element of the first set is not part of the second set. |

isSubset : Set<‘T> → Set<‘T> → bool | This expression evaluates to true if the first set contains all the elements in the second set as well. |

isSuperset : Set<‘T> → Set<‘T> → bool | There is a true value if all elements of the second set match the elements of the first set. |

iter : (‘T → unit) → Set<‘T> → unit | Each element of the set is applied with the given function in accordance with the comparison function, in numerical order. |

map : (‘T → ‘U) → Set<‘T> → Set<‘U> | In this function, the input set is converted into a collection of results that contain the results of applying the provided function to each element of that collection. |

maxElement : Set<‘T> → ‘T | According to the order in which the elements of the set are ordered, this function returns the highest element in the set. |

minElement : Set<‘T> → ‘T | According to the order in which the elements of the set are ordered, this function returns the Lowest element in the set. |

ofArray : ‘T array → Set<‘T> | It creates a set of elements that contain the same elements that are in the array that is given. |

ofList : ‘T list → Set<‘T> | This function creates a set with the same elements that are present in the given list. |

ofSeq : seq<‘T> → Set<‘T> | A new collection will be created from the given enumerable object. |

partition : (‘T → bool) → Set<‘T> → Set<‘T> * Set<‘T> | A set is split into two sets, which contain the elements which are returned true and false when the given predicate is applied. |

remove : ‘T → Set<‘T> → Set<‘T> | The function returns a new set that has the given element removed from it. If the set does not contain the element specified, no exception is raised. |

singleton : ‘T → Set<‘T> | Set containing an element of the given type. |

toArray : Set<‘T> → ‘T array | In this method, an array containing all elements of the set is created. |

toList : Set<‘T> → ‘T list | In this method, a list containing all elements of the set is created. |

toSeq : Set<‘T> → seq<‘T> | In this method, a sequence containing all elements of the set is created. |

union : Set<‘T> → Set<‘T> → Set<‘T> | Calculates the union between the two sets. |

unionMany : seq<Set<‘T>> → Set<‘T> | This function performs the union of multiple sets. |

Below are some examples that demonstrate the above properties of the set:

#### Example:

And the result of the following properties of sets are:

My Set: set [1; 2; 3; 4; 5; 6; 7; 8] My Set contains 4: true Updated Set: set [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] Union is: set [1; 2; 3; 4; 5; 6; 7; 8] Difference is: set [1; 3; 5; 7] Intersection: set [2; 4; 6; 8] Is set2 is subset of set1: true

### Example Explanation

This F# example creates two sets of integers using the **Set.ofList** function and then performs a variety of set operations on them.

Here’s what each part of the code does:

**let set1 = Set.ofList [1; 2; 3; 4; 5; 6; 7; 8;]**: This line creates a new set called set1 that contains the integers 1 through 8.**printfn “My Set: %A” set1**: This line prints the contents of set1 to the console using the**%A**format specifier, which is used to print arrays and collections.**let contains = set1.Contains(4)**: This line checks if set1 contains the integer 4 using the**Contains**method, which returns a Boolean value. The result is stored in a variable called**contains**.**printfn “My Set contains 4: %b” contains**: This line prints whether or not set1 contains the integer 4 based on the result of the previous line.**let addIntoSet = set1.Add(9).Add(10)**: This line adds the integers 9 and 10 to set1 using the**Add**method, which creates a new set that contains the updated elements. The result is stored in a variable called**addIntoSet**.**printfn “Updated Set: %A” addIntoSet**: This line prints the contents of the updated set that includes the integers 1 through 10.**let set2 = Set.ofList [2; 4; 6; 8]**: This line creates a new set called set2 that contains the even integers between 2 and 8.**let union = Set.union set1 set2**: This line performs the union of set1 and set2 using the**union**function, which creates a new set that contains all the unique elements from both sets. The result is stored in a variable called union.- printfn
**“Union is: %A” union**: This line prints the contents of the union set. - let
**diff = Set.difference set1 set2**: This line performs the set difference operation between set1 and set2 using the difference function, which creates a new set that contains the elements from set1 that are not in set2. The result is stored in a variable called**diff**. - printfn
**“Difference is: %A” diff**: This line prints the contents of the set difference. - let
**intersection = Set.intersect set1 set2**: This line performs the set intersection operation between set1 and set2 using the**intersect**function, which creates a new set that contains the elements that are common to both sets. The result is stored in a variable called**intersection**. - printfn
**“Intersection: %A” intersection**: This line prints the contents of the set intersection. - let
**isSubset = set2.IsSubsetOf(set1)**: This line checks whether set2 is a subset of set1 using the**IsSubsetOf**method, which returns a Boolean value. The result is stored in a variable called**isSubset**. - printfn
**“Is set2 is subset of set1: %b” isSubset**: This line prints whether or not set2 is a subset of set1 based on the result of the previous line.

You can help others learn about the power and flexible nature of F# functional programming language by sharing our article on social media below.This will enable them to create dynamic and interactive web applications.

## F# Sets Advantages

Sets in F# have several advantages that make them useful for programming. Here are some of the advantages:

- F# Sets are immutable, which means that we cannot modify the contents of a set once we create it. This immutability makes sets safe to use in a multi-threaded environment.
- F# Sets are implemented as balanced binary trees, which means that they have a logarithmic time complexity for many operations, such as insertions, deletions, and lookups.
- F# Sets are easy to use and provide a functional approach to programming. They support various operations that we can use to manipulate them without modifying their contents.