Routines (alphabetical) > Routines: U > UNIQ

# UNIQ

The UNIQ function returns the subscripts of the unique elements in an array. Note that repeated elements must be adjacent in order to be found. This routine is intended to be used with the SORT function: see the examples below. This function was inspired by the UNIX `uniq(1)` command.

This routine is written in the IDL language. Its source code can be found in the file `uniq.pro` in the `lib` subdirectory of the IDL distribution.

## Examples

Find the unique elements of an unsorted array:

; Create an array:
array = [1, 2, 1, 2, 3, 4, 5, 6, 6, 5]
; Variable B holds an array containing the sorted,
; unique values in array:
b = array[UNIQ(array, SORT(array))]
PRINT, b

IDL prints

1 2 3 4 5 6

## Syntax

Result = UNIQ( Array [, Index] )

## Return Value

UNIQ returns an array of indices into the original array. Note that the index of the last element in each set of non-unique elements is returned. If the optional Index argument is supplied, then the last index in the order provided by the Index array is returned.

The following expression returns a copy of the sorted array with duplicate adjacent elements removed:

Array[UNIQ(Array)]

UNIQ returns 0 (zero) if the argument supplied is a scalar rather than an array.

## Arguments

### Array

The array to be scanned. For UNIQ to work properly, the array must be sorted into monotonic order unless the optional parameter Index is supplied.

### Index

This optional parameter is an array of indices into Array that order the elements into monotonic order. That is, the expression:

Array[Index]

yields an array in which the elements of Array are rearranged into monotonic order.

The Index array can be generated from Array using the SORT function:

Index = SORT(Array)

If Array is not already in monotonic order, use the command:

UNIQ(Array, SORT(Array))

## Version History

Pre-4.0 | Introduced

## See Also

SORT, WHERE