SRL Archive - Help and Support

I need a function that removes duplicates from an array


WT-Fakawi

2011-02-03 10:04:09

what the title says:

Take f.i. this array:

s:= [1,1,1,1,4,4,9,9,9,10,1,12,15,15,15,16,17,19,19,21,21,24,24,24,25,26,27];

I need a function that removes the doubles from this array.


s:= [1,4,9,10,12,15,16,17,19,21,24,25,26,27];


Anyyone? It has gotta be easy, but I can't seem to get it properly implemented 0_o

Frement

2011-02-03 10:19:55

EDITED WHOLE POST:
program new;

var s, d: TVariantArray;

function array_unique(src: TVariantArray): TVariantArray;
var I, O, E: Integer;
Skip: Boolean;
begin
SetArrayLength(Result, 1);
Skip := False;
E := 0;
for I := 0 to High(src) do begin
for O := 0 to High(Result) do begin
if (Result[O] = src[I]) then begin
Skip := True;
break;
end;
end;
if not (Skip) then begin
SetArrayLength(Result, E+1);
Result[E] := src[I];
Inc(E);
end;
Skip := False;
end;
end;

begin
s := [1,1,1,1,4,4,9,9,9,10,1,12,15,15,15,16,17,19,19,21,21,24,24,24,25,26,27];
d := array_unique(s);
Writeln(ToStr(d));
end.

Had some errors, heres functional function :)

mastaraymond

2011-02-03 10:33:16


program new;

function remove_dups(ints : TIntegerArray) : TIntegerArray;
var
i,len,c,last : integer;
begin
len := length(ints)-1;
if len = 0 then exit;
setlength(result,len+1);
last := ints[0]+1;
Quicksort(ints);
for i := 0 to len do
if last <> ints[i] then
begin
result[c] := ints[i];
last := ints[i];
inc(c);
end;
setlength(result,c);
end;

var
input : TIntegerArray;
begin
input := [1,2,3,4,5,1,2,3,4,5,10,13,15,26,76,31];
Writeln(remove_dups(input));
end.

Downside, does sort the array…

Markus

2011-02-03 11:27:46

My go, complexity 1n but it trades that for a bit of memory usage (worst case 256mb? not recommended for colours but just for values < 500k);

program RemoveDups;

function RemoveDupsFromIntArr(input : TIntegerArray) : TIntegerArray;
var
Dupez : TBoolArray;
i, MaxDupe, ResIndex, K : integer;
begin
result := []; //Clear result, might make a mess
MaxDupe := -1;
ResIndex := 0;
K := High(input);
for i := 0 to K do
begin
if (input[i] > MaxDupe) then
begin
SetLength(Dupez, input[i]+1);
MaxDupe := input[i];
Dupez[input[i]] := True;
SetLength(result, resindex+1);
Result[ResIndex] := input[i];
Inc(ResIndex);
end else
if (not(Dupez[input[i]])) then
begin
Dupez[input[i]] := True;
SetLength(result, resindex+1);
Result[ResIndex] := input[i];
Inc(ResIndex);
end;
end;
end;
var i : integer;
begin
for i := 0 to 10000 do
(RemoveDupsFromIntArr([1,1,15,6,7,2,1,123456]));
writeln('Speed = '+FloatToStr(GetTimeRunning / 10000.0));
end.
Takes 0.8ms at my pc:)

I'd take Ray's since his is 10 times as fast :p Unless array sorting matters of course.

nielsie95

2011-02-03 11:29:32

Simba has a function ClearSameIntegers.

Frement

2011-02-03 11:31:35

From: nielsie95
Simba has a function ClearSameIntegers.


My function can be used for string arrays too, so its better?

nielsie95

2011-02-03 11:43:23

It's better if you want to use it for TVariantArrays, because Simba doesn't have a function for that. But it's always better to use a built in function as it should be faster ;)

WT-Fakawi

2011-02-03 11:55:44

From: nielsie95
Simba has a function ClearSameIntegers.

Hahaha.

Thank you all for your input. Much appreciated. Reminds me of the "Hello World" thread by Starblaster!

Janilabo

2011-02-03 12:14:29

Haha, very nice functions all. :)

I know there was already solution to this problem in here, but I just must be part of this topic.. :p

So, here is my procedure, I have been using it already for a while (scripting to another MMORPG using my own include):

procedure deleteDuplicateIntegers(var TIA: TIntegerArray);
var
i, i2, h, t: Integer;
tmpTIA: TIntegerArray;
b, za: Boolean;
begin
h:= High(TIA);
SetArrayLength(tmpTIA, (h + 1));
for i:= 0 to h do
begin
for i2:= 0 to t do
begin
b:= (TIA[i] = tmpTIA[i2]);
if(TIA[i] = 0)then
if(not(za))then
begin
za:= True;
Break;
end;
if(b)then
Break;
end;
if(not(b)or(i = 0))then
begin
tmpTIA[t]:= TIA[i];
Inc(t);
end else
b:= False;
end;
SetArrayLength(TIA, 0);
SetArrayLength(tmpTIA, t);
TIA:= tmpTIA;
SetArrayLength(tmpTIA, 0);
end;

BraK

2011-02-03 12:32:12

From: Janilabo
Haha, very nice functions all. :)

I know there was already solution to this problem in here, but I just must be part of this topic.. :p

So, here is my procedure, I have been using it already for a while (scripting to another MMORPG using my own include):



What MMO? I'm currently looking into Making some scripts For Aika.

~BraK

Janilabo

2011-02-03 13:03:38

From: BraK
What MMO? I'm currently looking into Making some scripts For Aika.

~BraK
Ello BraK,

The game I am scripting for is called as "Shadow Worlds", it's really not an MMORPG though, more like MORPG, because of the amount of players. :D
The gameplay in it isn't too good at all, but it is a great game for scripting purposes!

Hmmm… Aika, I think I have heard about that game.. It sounds really familiar.
Google'd a bit and it does have pretty nice graphics. :)

HERE is my include project I have been (randomly) working for a bit over 2 weeks now, if you are interested to check it out..