Expand/Shrink

unique

Definition: sequence res = unique(sequence s, string options="SORT")
Description: Returns s with duplicate elements removed.

s: the sequence from which to remove duplicates.
options: "SORT" (the default) performs a standard sort on s before removing duplicates,
"PRESORTED" indicates the initial sort is unnecesary,
"STABLE" (==="INPLACE") retains the original order of any unique entries, but uses a slightly slower dictionary-based method of removing duplicates.
A fatal error occurs if options is not one of those (or not all upper-case).

If "PRESORTED" is specified, but s is not actually sorted, then only adjacent duplicates are removed.

Note that under with javascript_semantics result = unique(result) should be replaced with result = unique(deep_copy(result)) when result is not routine-local, and similarly when res and s are different variables, that is unless options is "PRESORTED" or "STABLE"/"INPLACE".
pwa/p2js: Supported, may however trigger p2js violations, that usually need unique(deep_copy(s)) to fix.
Example:
sequence s = { 4,7,9,7,2,5,5,9,0,4,4,5,6,5}
?unique(s, "STABLE")            --=> {4,7,9,2,5,0,6}
?unique(s, "SORT")              --=> {0,2,4,5,6,7,9}
?unique(s, "PRESORTED")         --=> {4,7,9,7,2,5,9,0,4,5,6,5}
?unique(sort(s), "PRESORTED")   --=> {0,2,4,5,6,7,9}
Implementation: See builtins\punique.e (an autoinclude) for details of the actual implementation.
Expand/Shrink