Search code examples
arraysdelphidynamic-arraysstatic-array

Remove duplicate array elements


I need to remove all duplicate values from an array of integer, yet maintain the order of the elements:

Example:

10,20,20(duplicate),10(duplicate),50

Becomes:

10,20,50

Solution

    1. Create a dictionary with Integer as the key. The value type is immaterial.
    2. Iterate through the input array. For each value in the input array, check whether or not that value is in the dictionary.
    3. If yes, this is a duplicate, discard.
    4. If no, this is the first time the value has been encountered. Retain the value, and add it to the dictionary.

    The point of the dictionary is that it can perform O(1) lookup.

    In pseudocode:

    var
      arr: TArray<Integer>; // input and output
      Dict: TDictionary<Integer, Integer>;
      SrcIndex, DestIndex: Integer;
    ....
    DestIndex := 0;
    for SrcIndex := 0 to high(arr) do begin
      Value := arr[SrcIndex];
      if not Dict.ContainsKey(Value) then begin
        arr[DestIndex] := arr[SrcIndex];
        Dict.Add(Value, 0);
        inc(DestIndex);
      end;
    end;
    SetLength(arr, DestIndex);
    

    Obviously you need to create, and destroy, the dictionary. I'm assuming you know how to do that. And I've opted to modify the array in place but you could equally create a new array if you prefer.