set_unique

arrayfire.set_unique(array: Array, /, *, is_sorted: bool = False) Array

Extracts unique elements from a 1D ArrayFire array.

This function returns a new array containing only the unique elements of the input array. It can operate more efficiently if the input array is known to be sorted.

Parameters

arrayArray

The input 1D ArrayFire array from which unique elements are to be extracted.

is_sortedbool, optional, keyword-only, default: False

Indicates whether the input array is already sorted. If True, the function can skip the sorting step, potentially improving performance. However, setting this to True for an unsorted array will lead to incorrect results.

Returns

Array

An ArrayFire array containing the unique elements extracted from the input array.

Examples

>>> import arrayfire as af
>>> a = af.Array([1, 2, 2, 3, 4, 4, 5, 5, 5])
>>> af.set_unique(a)
[5 1 1 1]
    1.0000
    2.0000
    3.0000
    4.0000
    5.0000
>>> sorted_a = af.sort(a)  # Assuming 'a' is not sorted
>>> af.set_unique(sorted_a, is_sorted=True)
[5 1 1 1]
    1.0000
    2.0000
    3.0000
    4.0000
    5.0000

Note

The input array must be 1D. If you have a multi-dimensional array, consider reshaping or flattening it before using this function. Ensure the is_sorted flag accurately reflects the state of the input array to avoid incorrect results.