removeDuplicates()

Synopsis

Returns a new list containing a single occurrence of each duplicated item in the original list . The order of the items in the returned list is not defined. Non-duplicated items also appear in the returned list . There is no way to specify which occurrences of the duplicates are removed.

Syntax

RemoveDuplicates ( list As List, _
                   Optional key As Name = :Identity, _
                   Optional test As Name = :Equal ) As List 
Argument Type Description
list List The list from which to remove duplicates.
key Name Optional; name of a function to be applied to each item when getting its value during then check for duplicates; default is :Identity.
test Name Optional; name of a function to be applied to each item when comparing two values; default is :Equal.

Example 1

Homogeneous types
Intent >RemoveDuplicates({4, 7, 4, 2, 6, 6, 3, 1, 3}) 
--> {4, 7, 2, 6, 3, 1} 

Example 2

Heterogeneous types
Intent >RemoveDuplicates({:a, :z, :c, :a, 3, :e, :f, :g, :c}) 
--> {:a, :z, :c, 3, :e, :f, :g} 

Example 3

Using a test function
Intent >RemoveDuplicates({:a, :b, 3, "a", "b", {3}, "3"}, test := :sameType?) 
--> {:a, 3, "a", {3}} 
Using a custom function as the test for duplicates, you could also keep just one entry of each data Type, regardless of value.
Function sameType?(I as Any, j as Any) as Boolean
    sameType? = (typeName(i) = typeName(j)) 
End Function 

Example 4

Using a key function
Intent >RemoveDuplicates({1.4, 2.5, 2.6, 3.5, 1.1, 2.3}, key := :ceiling) 
--> {1.4, 2.5, 3.5} 
Result is not necessarily in this order and not necessarily the exact elements. For example, instead of 2.5, 2.6 or 2.3 may be in the list .

Example 5

Recursive, works with multiple list .
Intent >RemoveDuplicates({{4.5,6},{2.5,6},{1.5,8},{4.5,6}})
--> {{4.5, 6}, {2.5, 6}, {1.5, 8}}