# NumPy Set Operations

## What is a Set

A set in mathematics is a collection of unique elements.

Sets are used for operations involving frequent intersection, union and difference operations.

## Create Sets in NumPy

We can use NumPy’s `unique()` method to find unique elements from any array. E.g. create a set array, but remember that the set arrays should only be 1-D arrays.

### Example

Convert following array with repeated elements to a set:

Try it Yourself »

## Finding Union

To find the unique values of two arrays, use the `union1d()` method.

### Example

Find union of the following two set arrays:

Try it Yourself »

## Finding Intersection

To find only the values that are present in both arrays, use the `intersect1d()` method.

### Example

Find intersection of the following two set arrays:

Try it Yourself »

Note: the `intersect1d()` method takes an optional argument `assume_unique`, which if set to True can speed up computation. It should always be set to True when dealing with sets.

## Finding Difference

To find only the values in the first set that is NOT present in the seconds set, use the `setdiff1d()` method.

### Example

Find the difference of the set1 from set2:

Try it Yourself »

Note: the `setdiff1d()` method takes an optional argument `assume_unique`, which if set to True can speed up computation. It should always be set to True when dealing with sets.

## Finding Symmetric Difference

To find only the values that are NOT present in BOTH sets, use the `setxor1d()` method.

### Example

Find the symmetric difference of the set1 and set2:

Try it Yourself »

Note: the `setxor1d()` method takes an optional argument `assume_unique`, which if set to True can speed up computation. It should always be set to True when dealing with sets.