KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility method to remove duplicate elements in an array
PRODUCT: 4D | VERSION: 15.x | PLATFORM: Mac & Win
Published On: April 13, 2017

Below is a utility method that removes duplicate elements within an array. The results are returned in a separate array.

// UTIL_ARR_REMOVE_DUPLICATES
// $1 - Pointer to source array
// $2 - Pointer to array to contain results

C_POINTER($1;$arr;$2;$newArr)
C_LONGINT($i)
$arr:=$1
$newArr:=$2
For ($i;1;Size of array($arr->))
   If (Find in array($newArr->;$arr->{$i})=-1)
      APPEND TO ARRAY($newArr->;$arr->{$i})
   End if
End for


The source array and the return array should be of the same type. Here is an example of using the method:
ARRAY LONGINT($arr;5)
$arr{1}:=1
$arr{2}:=1
$arr{3}:=2
$arr{4}:=3
$arr{5}:=1

ARRAY LONGINT($newArr;0)
UTIL_ARR_REMOVE_DUPLICATES (->$arr;->$newarr)


The results are: