# F Sharp Programming/Sets and Maps

In addition to lists and sequences, F# provides two related immutable data structures called sets and maps. Unlike lists, sets and maps are unordered data structures, meaning that these collections do not preserve the order of elements as they are inserted, nor do they permit duplicates.

## Sets

A set in F# is just a container for items. Sets do not preserve the order in which items are inserted, nor do they allow duplicate entries to be inserted into the collection.
Sets can be created in a variety of ways:
Adding an item to an empty set The Set module contains a useful function Set.empty which returns an empty set to start with.
Conveniently, all instances of sets have an Add function with the type val Add : 'a -> Set<'a>. In other words, our Add returns a new set containing our new item, which makes it easy to add items together in this fashion:
Converting lists and sequences into sets Additionally, the we can use Set.ofList and Set.ofSeq to convert an entire collection into a set:
The example above demonstrates the unordered nature of sets.

### The Set Module

The FSharp.Collections.Set module contains a variety of useful methods for working with sets.
val add : 'a -> Set<'a> -> Set<'a>
    Return a new set with an element added to the set. No exception is raised if the set already contains the given element.
val compare : Set<'a> -> Set<'a> -> int
    Compare two sets. Places sets into a total order.
val count : Set<'a> -> int
    Return the number of elements in the set. Same as "size".
val difference : Set<'a> -> Set<'a> -> Set<'a>
    Return a new set with the elements of the second set removed from the first. That is a set containing only those items from the first set that are not also in the second set.
val exists : ('a -> bool) -> Set<'a> -> bool
    Test if any element of the collection satisfies the given predicate.
val filter : ('a -> bool) -> Set<'a> -> Set<'a>
    Return a new collection containing only the elements of the collection for which the given predicate returns "true".
val intersect : Set<'a> -> Set<'a> -> Set<'a>
    Compute the intersection, or overlap, of the two sets.
val map : ('a -> 'b) -> Set<'a> -> Set<'b>
    Return a new collection containing the results of applying the given function to each element of the input set.
val contains: 'a -> Set<'a> -> bool
    Evaluates to true if the given element is in the given set.
val remove : 'a -> Set<'a> -> Set<'a>
    Return a new set with the given element removed. No exception is raised if the set doesn't contain the given element.
val count: Set<'a> -> int
    Return the number of elements in the set.
val isSubset : Set<'a> -> Set<'a> -> bool
    Evaluates to "true" if all elements of the first set are in the second.
val isProperSubset : Set<'a> -> Set<'a> -> bool
    Evaluates to "true" if all elements of the first set are in the second, and there is at least one element in the second set which is not in the first.
val union : Set<'a> -> Set<'a> -> Set<'a>
    Compute the union of the two sets.

### Examples
## Maps

A map is a special kind of set: it associates keys with values. A map is created in a similar way to sets:
You can use the .[key] to access elements in the map:

### The Map Module

The FSharp.Collections.Map module handles map operations.
val add : 'key -> 'a -> Map<'key,'a> -> Map<'key,'a>
    Return a new map with the binding added to the given map.
val empty<'key,'a> : Map<'key,'a>
    Returns an empty map.
val exists : ('key -> 'a -> bool) -> Map<'key,'a> -> bool
    Return true if the given predicate returns true for one of the bindings in the map.
val filter : ('key -> 'a -> bool) -> Map<'key,'a> -> Map<'key,'a>
    Build a new map containing only the bindings for which the given predicate returns true.
val find : 'key -> Map<'key,'a> -> 'a
    Lookup an element in the map, raising KeyNotFoundException if no binding exists in the map.
val containsKey: 'key -> Map<'key,'a> -> bool
    Test if an element is in the domain of the map.
val remove : 'key -> Map<'key,'a> -> Map<'key,'a>
    Remove an element from the domain of the map. No exception is raised if the element is not present.
val tryFind : 'key -> Map<'key,'a> -> 'a option
    Lookup an element in the map, returning a Some value if the element is in the domain of the map and None if not.

### Examples
## Set and Map Performance

F# sets and maps are implemented as immutable AVL trees, an efficient data structure which forms a self-balancing binary tree. AVL trees are well-known for their efficiency, in which they can search, insert, and delete elements in the tree in O(log n) time, where n is the number of elements in the tree.