Skip to content

Latest commit

 

History

History
74 lines (49 loc) · 2.38 KB

File metadata and controls

74 lines (49 loc) · 2.38 KB

DeDup<T> class method

Project: Array Utilities Unit

Unit: DelphiDabbler.Lib.ArrayUtils

Record: TArrayUtils

Applies to: ~>0.1

class function DeDup<T>(const A: array of T;
  const AEqualityComparer: TEqualityComparison<T>): TArray<T>;
  overload; static;

class function DeDup<T>(const A: array of T;
  const AEqualityComparer: IEqualityComparer<T>): TArray<T>;
  overload; static;

class function DeDup<T>(const A: array of T): TArray<T>;
  overload; static;

Description

Returns a copy of an array that contains no duplicated elements.

The copy is a shallow copy, so any references within the resulting array are the same as those in the initial array.

Parameters:

  • A - The array to be de-duplicated.

  • AEqualityComparer - An optional function or object that is used to test the equality of two values. Used to determine which array elements are duplicated.

    If AEqualityComparer is provided it must be one of:

    If the parameter is omitted then the default equality comparer defined by Delphi's TEqualityComparer<T>.Default method is used.

Returns:

  • The de-duplicated copy of the array.

Note

If a deep copy of the de-duplicated array is required then pass the return value of DeDup<T> to the deep copying overload of Copy<T> along with a suitable TCloner<T> implementation.

Example

To de-duplicate a string array use the following code. The code uses the equality comparer function overloaded version of DeDup<T>.

procedure DeDup_Eg;
var
  A, B, Expected: TArray<string>;
begin
  A := TArray<string>.Create('Foo', 'Bar', 'foo', 'Foo', 'BAR');
  B := TArrayUtils.DeDup<string>(A, SameText);
  Expected := TArray<string>.Create('Foo', 'Bar');
  Assert(TArrayUtils.Equal<string>(Expected, B, SameText));
end;

See Also