# Anyone have good code to get difference of two TList<T>

## Recommended Posts

Have not done much Generics stuff, but the trivial stuff.

I think this must be class of some sort, right? To get the/return T

Something like :  (This could be actual helper also, maybe??)

TGenericListHelper<T> = class(TObject)

public

function Diffference(const AList1, AList2: TLIst<T>): TList<T>;

end;

Something like that. And then add rest.  I'll try to implement that on my own but if someone has good code/ideas for that, let me know.

Got it. Not super good but seems to work,

TGenericListHelper = class(TObject)
public
class function Difference<T>(const AList1, AList2: TList<T>): TList<T>;
end;

class function TGenericListHelper.Difference<T>(const AList1, AList2: TList<T>): TList<T>;
var
I: Integer;
begin
Result := TList<T>.Create;

for I := 0 to AList1.Count - 1 do
if AList2.IndexOf(AList1) = -1 then

for I := 0 to AList2.Count - 1 do
if AList1.IndexOf(AList2) = -1 then
end;

Surely with sorting etc can make this faster and most likely there is better algorithm for this. If you know how to make this better, I am all ears.

-Tee-

Have you checked Spring4D ?

The solution heavily depends on what you call different on these lists. Your solution simply makes the lists containing the same elements, which is quite off checking for equality. Others might see two lists equal if the elements and their order match. Also equality of two elements can mean different things depending on the actual type T. At least TList<T> offers to provide a comparer for the latter.

1 hour ago, Rollo62 said:

Have you checked Spring4D ?

I is too large of an library to add into our SVN for such a little use cases, at least so far, Could check for the algorithm tough.

33 minutes ago, Uwe Raabe said:

The solution heavily depends on what you call different on these lists. Your solution simply makes the lists containing the same elements, which is quite off checking for equality. Others might see two lists equal if the elements and their order match. Also equality of two elements can mean different things depending on the actual type T. At least TList<T> offers to provide a comparer for the latter.

True dat. I am interested in the values, not the order.

At http://www.angusj.com/delphi/ there a TDiff for textfiles. However, the textfiles are CRC:ed int two integer lists that are compared. It uses "An O(NP) Sequence Comparison Algorithm".

The thing is re-implemented in mORMot.

I have a suspicion that it could be interesting to look at either implementation.

/D

• 1

## Create an account

Register a new account

×