All Products
Search
Document Center

MaxCompute:ARRAY_DISTINCT

Last Updated:Jul 26, 2023

Removes duplicate elements from Array a.

Syntax

array<T> array_distinct(array<T> <a>)

Parameters

a: required. This parameter specifies an array. T in array<T> specifies the data type of the elements in the array. The elements can be of any data type.

Return value

A value of the ARRAY type is returned. The return value varies based on the following rules:

  • The returned array has no duplicate elements, and the elements are sorted in the same order as those in Array a.

  • If an element in Array a is null, the null element is involved in the operation.

  • If the input array is empty, an empty array is returned.

Examples

  • Example 1: Remove duplicate elements from array(10, 20, 30, 30, 20, 10). Sample statement:

    -- The return value is [10,20,30]. 
    select array_distinct(array(10, 20, 30, 30, 20, 10));
  • Example 2: Remove duplicate elements from array(10, 20, 20, null, null, 30, 20, null). Sample statement:

    -- The return value is [10,20,null,30]. 
    select array_distinct(array(10, 20, 20, null, null, 30, 20, null)); 
  • Example 3: Remove duplicate elements from an empty array. Sample statement:

    -- The return value is []. 
    select array_distinct(array());

Related functions

ARRAY_DISTINCT is a complex type function. For more information about the functions that are used to process data of complex data types, such as ARRAY, MAP, STRUCT, and JSON, see Complex type functions.