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 in the lib subdirectory of the IDL distribution.


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))]

IDL prints

1    2    3    4    5    6


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:


UNIQ returns 0 (zero) if the argument supplied is a scalar rather than an 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.


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


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



See Also