Delete the same array of elements in delphi

I am trying to delete the same array element in delphi. Examples:
R[1] := 33332111111111111111111111323333333334378777433333344333333333277

I want to make it 32132343787434327. and save it in a new array.
Could you give some idea?

I already tried to make each of the elements of R [1] in Array. And tried some code.

  for i:=1 to length(NR) do begin found:=false; for k:=i+1 to length(NR) do begin if (NR[i]=NR[k]) then begin found:=true; end; end; if (not found) then begin Memo1.Lines.Add(NR[i]); end; end; 

But the result is 184327.
could you guys help me? thank you very much. I so desperately want to do this.

+6
source share
1 answer

It seems that you are working with strings, not arrays. In this case, you will need this function:

 function RemoveAdjacentDuplicates(const X: string): string; var i, j: Integer; begin SetLength(Result, Length(X)); j := 0; for i := 1 to Length(Result) do if (i=1) or (X[i]<>X[i-1]) then begin inc(j); Result[j] := X[i]; end; SetLength(Result, j); end; 

Do it.

First of all, I highlight the result variable. This is likely to be an excess. We know that the result cannot be more than input.

We use two indexing local variables, rather weakly named i and j . We could give them descriptive names, but for such a short function, we could decide that this is not necessary. Feel free to come up with other names if you want. For example, you can choose idxIn and idxOut .

One variable indexes the input, and the other indexes the output. The input index is used in a simple loop. The output index increases every time we find a unique element.

The if condition checks if the input index refers to a character that is different from the previous one. The first element does not have the previous element, so we always include it.

As soon as the cycle ends, we know how long the output is and can complete the final distribution.

Adapting this for an array is simple. You just need to consider arrays using null indices. For a bit of fun, here is the general version for arrays:

 type TMyArrayHelper = class class function RemoveAdjacentDuplicates<T>(const X: array of T): TArray<T>; static; end; class function TMyArrayHelper.RemoveAdjacentDuplicates<T> (const X: array of T): TArray<T>; var i, j: Integer; Comparer: IEqualityComparer<T>; begin Comparer := TEqualityComparer<T>.Default; SetLength(Result, Length(X)); j := 0; for i := 0 to high(Result) do if (i=0) or not Comparer.Equals(X[i], X[i-1]) then begin Result[j] := X[i]; inc(j); end; SetLength(Result, j); end; 

Note the subtle inc(j) layout. This is necessary to switch to zero indexing.

A slightly more complex alternative with fewer tests would be:

 class function TMyArrayHelper.RemoveAdjacentDuplicates<T> (const X: array of T): TArray<T>; var i, j, len: Integer; Comparer: IEqualityComparer<T>; begin Comparer := TEqualityComparer<T>.Default; len := Length(X); SetLength(Result, len); if len=0 then exit; Result[0] := X[0]; j := 1; for i := 1 to len-1 do if not Comparer.Equals(X[i], X[i-1]) then begin Result[j] := X[i]; inc(j); end; SetLength(Result, j); end; 
+14
source

All Articles