> ## Documentation Index
> Fetch the complete documentation index at: https://numpyts.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Set Operations

> Unique values, membership testing, set intersection, union, difference, symmetric difference, and zero trimming.

### unique

Find the sorted unique elements of an array. Optionally return indices, inverse mapping, and counts.

```typescript theme={null}
function unique(
  ar: NDArray,
  returnIndex?: boolean,
  returnInverse?: boolean,
  returnCounts?: boolean,
  axis?: number
): NDArray | { values: NDArray; indices?: NDArray; inverse?: NDArray; counts?: NDArray; }
```

| Name | Type | Default | Description |
| - | - | - | - |
| `ar` | `NDArray` | -- | Input array. It is flattened before computing unique values unless `axis` is specified. |
| `returnIndex` | `boolean` | `false` | If `true`, also return the indices of the first occurrences. |
| `returnInverse` | `boolean` | `false` | If `true`, also return the indices to reconstruct the original from the unique array. |
| `returnCounts` | `boolean` | `false` | If `true`, also return the number of times each unique value appears. |
| `axis` | `number` | `undefined` | Axis along which to find unique slices. If provided, the array is not flattened; instead unique slices along the given axis are found. |

**Returns:** `NDArray` when no optional flags are `true`. Object `{ values, indices?, inverse?, counts? }` when one or more flags are `true`.

```typescript theme={null}
import * as np from 'numpy-ts';

const a = np.array([3, 1, 2, 1, 3, 2, 3]);

np.unique(a);
// array([1, 2, 3])

const result = np.unique(a, true, true, true);
console.log(result.values.toString());   // array([1, 2, 3])
console.log(result.indices!.toString()); // first occurrence positions
console.log(result.inverse!.toString()); // reconstruct original
console.log(result.counts!.toString());  // occurrence counts

// Unique rows (axis=0)
const m = np.array([[1,2,3],[4,5,6],[1,2,3],[7,8,9]]);
const uniq = np.unique(m, false, false, false, 0);
console.log(uniq.shape);  // [3, 3]  (duplicate row removed)

// Unique columns (axis=1)
const b = np.array([[1,1,2],[3,3,4],[5,5,6]]);
const uniqCols = np.unique(b, false, false, false, 1);
console.log(uniqCols.shape);  // [3, 2]
```

***

### unique\_all

Return sorted unique values along with their first-occurrence indices, inverse mapping, and counts. This is a structured-return alternative to calling `unique` with all flags set to `true`.

```typescript theme={null}
function unique_all(x: NDArray): {
  values: NDArray;
  indices: NDArray;
  inverse_indices: NDArray;
  counts: NDArray;
}
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x` | `NDArray` | -- | Input array. |

**Returns:** An object with four properties: `values` (sorted unique elements), `indices` (first-occurrence indices), `inverse_indices` (mapping to reconstruct input), and `counts` (element counts).

```typescript theme={null}
import * as np from 'numpy-ts';

const result = np.unique_all(np.array([3, 1, 2, 1, 3]));
// result.values          = array([1, 2, 3])
// result.indices         = array([1, 2, 0])
// result.inverse_indices = array([2, 0, 1, 0, 2])
// result.counts          = array([2, 1, 2])
```

***

### unique\_counts

Return sorted unique values and their counts.

```typescript theme={null}
function unique_counts(x: NDArray): {
  values: NDArray;
  counts: NDArray;
}
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x` | `NDArray` | -- | Input array. |

**Returns:** An object with `values` (sorted unique elements) and `counts` (number of occurrences of each).

```typescript theme={null}
import * as np from 'numpy-ts';

const result = np.unique_counts(np.array([1, 1, 2, 3, 3, 3]));
// result.values = array([1, 2, 3])
// result.counts = array([2, 1, 3])
```

***

### unique\_inverse

Return sorted unique values and the inverse mapping to reconstruct the original array.

```typescript theme={null}
function unique_inverse(x: NDArray): {
  values: NDArray;
  inverse_indices: NDArray;
}
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x` | `NDArray` | -- | Input array. |

**Returns:** An object with `values` (sorted unique elements) and `inverse_indices` (indices into `values` that reconstruct the original).

```typescript theme={null}
import * as np from 'numpy-ts';

const result = np.unique_inverse(np.array([3, 1, 2, 1]));
// result.values          = array([1, 2, 3])
// result.inverse_indices = array([2, 0, 1, 0])
// Reconstruct: values[inverse_indices] -> [3, 1, 2, 1]
```

***

### unique\_values

Return only the sorted unique values (no indices, inverse, or counts).

```typescript theme={null}
function unique_values(x: NDArray): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `x` | `NDArray` | -- | Input array. |

**Returns:** `NDArray` -- Sorted unique values.

```typescript theme={null}
import * as np from 'numpy-ts';

np.unique_values(np.array([3, 1, 2, 1, 3]));
// array([1, 2, 3])
```

***

### in1d

> [!WARNING]
>
> **Deprecated in v1.3.0** to match NumPy 2.4. Use [`isin`](#isin) instead — `isin` preserves the input shape.

Test whether each element of a 1D array is also present in a second array. Returns a flattened boolean array.

```typescript theme={null}
function in1d(ar1: NDArray, ar2: NDArray): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `ar1` | `NDArray` | -- | Input array to test. |
| `ar2` | `NDArray` | -- | Values to test against. |

**Returns:** `NDArray` -- Boolean array of the same length as `ar1` (flattened).

```typescript theme={null}
import * as np from 'numpy-ts';

np.in1d(np.array([1, 2, 3, 4]), np.array([2, 4]));
// array([false, true, false, true])
```

***

### isin

Test whether each element of an array is present in a set of test elements. Unlike `in1d`, this preserves the shape of the input.

```typescript theme={null}
function isin(element: NDArray, testElements: NDArray): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `element` | `NDArray` | -- | Input array to test. |
| `testElements` | `NDArray` | -- | Values to test against. Flattened internally. |

**Returns:** `NDArray` -- Boolean array with the same shape as `element`.

```typescript theme={null}
import * as np from 'numpy-ts';

const a = np.array([[1, 2], [3, 4]]);
np.isin(a, np.array([2, 3]));
// array([[false, true],
//        [true, false]])
```

***

### intersect1d

Find the sorted intersection of two arrays.

```typescript theme={null}
function intersect1d(
  ar1: NDArray,
  ar2: NDArray
): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `ar1` | `NDArray` | -- | First input array. |
| `ar2` | `NDArray` | -- | Second input array. |

**Returns:** `NDArray` of sorted common values.

```typescript theme={null}
import * as np from 'numpy-ts';

np.intersect1d(np.array([1, 2, 3, 4]), np.array([3, 4, 5, 6]));
// array([3, 4])
```

***

### union1d

Find the sorted union of two arrays.

```typescript theme={null}
function union1d(ar1: NDArray, ar2: NDArray): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `ar1` | `NDArray` | -- | First input array. |
| `ar2` | `NDArray` | -- | Second input array. |

**Returns:** `NDArray` -- Sorted 1D array of unique values present in either input.

```typescript theme={null}
import * as np from 'numpy-ts';

np.union1d(np.array([1, 2, 3]), np.array([2, 3, 4, 5]));
// array([1, 2, 3, 4, 5])
```

***

### setdiff1d

Find the set difference: elements in `ar1` that are not in `ar2`.

```typescript theme={null}
function setdiff1d(ar1: NDArray, ar2: NDArray): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `ar1` | `NDArray` | -- | Input array. |
| `ar2` | `NDArray` | -- | Elements to exclude. |

**Returns:** `NDArray` -- Sorted 1D array of values in `ar1` that are not in `ar2`.

```typescript theme={null}
import * as np from 'numpy-ts';

np.setdiff1d(np.array([1, 2, 3, 4, 5]), np.array([2, 4]));
// array([1, 3, 5])
```

***

### setxor1d

Find the symmetric difference: elements that are in either of two arrays, but not in both.

```typescript theme={null}
function setxor1d(ar1: NDArray, ar2: NDArray): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `ar1` | `NDArray` | -- | First input array. |
| `ar2` | `NDArray` | -- | Second input array. |

**Returns:** `NDArray` -- Sorted 1D array of values in exactly one of the two inputs.

```typescript theme={null}
import * as np from 'numpy-ts';

np.setxor1d(np.array([1, 2, 3]), np.array([2, 3, 4]));
// array([1, 4])

np.setxor1d(np.array([1, 1, 2]), np.array([2, 3, 3]));
// array([1, 3])
```

***

### trim\_zeros

Trim leading and/or trailing zeros from a 1D array.

```typescript theme={null}
function trim_zeros(filt: NDArray, trim?: 'f' | 'b' | 'fb'): NDArray
```

| Name | Type | Default | Description |
| - | - | - | - |
| `filt` | `NDArray` | -- | 1D input array. |
| `trim` | `string` | `'fb'` | A string of `'f'` (front), `'b'` (back), or `'fb'` (both). Controls which end(s) to trim. |

**Returns:** `NDArray` -- The trimmed array.

```typescript theme={null}
import * as np from 'numpy-ts';

np.trim_zeros(np.array([0, 0, 1, 2, 3, 0, 0]));
// array([1, 2, 3])

np.trim_zeros(np.array([0, 0, 1, 2, 0]), 'f');
// array([1, 2, 0])  -- trim front only

np.trim_zeros(np.array([0, 0, 1, 2, 0]), 'b');
// array([0, 0, 1, 2])  -- trim back only
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.