Merge 2 Arrays excluding double entries

Hi there,

i hope someone here can help me with my Problem.
well, let's specify it.

i have a procedure, in which i want to merge 2 arrays into one (length(array1)+length(array2)). BUT i do not want to put double entries, which exist already in array1 to be filled from array2 into the result array.

can anyone help me with that?



(*===============================================*)

PROCEDURE Merge(array1, array2: ARRAY OF INTEGER; length1, length2: INTEGER;
VAR result: ARRAY OF INTEGER; VAR resultLength: INTEGER);
VAR
i, j, k : INTEGER;
resultCount : INTEGER;

BEGIN
i := 0;
j := 0;
k := 0;
resultCount := 0;

FOR i := 0 TO length1-1 DO BEGIN
result[i] := array1[i];
Inc(resultCount);
END;

FOR i := 0 TO length2-1 DO BEGIN
result[i+length1] := array2[i];
Inc(resultCount);
END;


resultLength := resultCount;

END;

Comments

  • may i specify too, that the array doesn't need to be sorted in any way.
  • You may want to reconsider how you attack this problem.
    Try the divide and conquer method rather than just brute force.

    1) Sort each array in either ascending or descending order
    using QuickSort(a unit I wrote that's in [blue][italic]21/5/2010[/italic][/blue] post
    in this forum: [b]Re: arranging sorting an array[/b])

    2) Now you can compare each element in each array before
    putting the element in the result array and skipping the
    duplicates.
  • thanks for your quick respons, although i must correct me.
    It MUSTN'T be sorted prior to merging it... I already did it with sorting it before, but i'm in dire need of an algorithm that does compare from 0 to length1 array1 = array2 (and if that is the case, the element shouldn't be added to result [not using pointers btw])

    greets
  • : thanks for your quick respons, although i must correct me.
    : It MUSTN'T be sorted prior to merging it... I already did it with
    : sorting it before, but i'm in dire need of an algorithm that does
    : compare from 0 to length1 array1 = array2 (and if that is the case,
    : the element shouldn't be added to result [not using pointers btw])
    :
    [code][color=Blue]{$type fpc}

    type ai=array of integer;


    procedure merge(a1,a2:ai;var a3:ai);
    var l1,l2,l3:word; // <-- the length of each array
    i,j:word; // <-- counters
    trig:boolean; // <-- trigger
    begin
    l1:=succ(high(a1));
    l2:=succ(high(a2)); // get input array lengths
    a3:=copy(a1,0,l1); // copy first array into output
    setlength(a3,l1+l2); // reserve memory for output
    l3:=l1; // set index for a3
    for i:=0 to pred(l2) do begin // going through a2 one element at a time
    trig:=true; // set trigger
    for j:=0 to pred(l1) do // going through a1
    if a2[i]=a1[j] then trig:=false; // check if element a2 matches anything from a1
    if trig then begin // if no match ( according to trigger )
    a3[l3]:=a2[i]; // add element to a3
    inc(l3); // increase index
    end;
    end;
    setlength(a3,l3); // trim unused memory off a3
    end;


    var a1,a2,a3:ai;
    i:word;

    begin
    randomize;
    setlength(a1,10); setlength(a2,10);
    for i:=0 to 9 do begin
    a1[i]:=random(11);a2[i]:=random(11);
    end;
    write('a1= ');for i:=0 to 8 do write(a1[i],',');writeln(a1[9]);
    write('a2= ');for i:=0 to 8 do write(a2[i],',');writeln(a2[9],#13#10);
    merge(a1,a2,a3);
    write('a3= ');for i:=0 to pred(high(a3)) do write(a3[i],',');writeln(a3[high(a3)]);
    readln;
    end.[/color][/code]
  • Or you could try something like this:
    [code] var
    noeia1, { number of elements in array 1 }
    noeia2, { number of elements in array 2 }
    noeira : word; { number of elements in result array }
    (*
    a : array[1..noeia1] of integer; { 1st array }
    b : array[1..noeia2] of integer; { 2nd array }
    r : array[1..noeira] of integer; { result array }
    *)
    i, j, k : word;
    sae : integer; { second array element }
    quit : boolean;

    begin

    for i := 1 to noeia1 do { move all of 1st array to result }
    r[i] := a[i];

    k := noeia1;
    i := 0;

    repeat
    Inc( i );
    sae := b[i]; { get an element of the 2nd array }
    j := 0;

    repeat
    Inc( j );
    quit := a[j] = sae { compare 1st array element to 2nd }
    until quit or (j = noeia1);

    if not quit then begin
    Inc( k );
    r[k] := sae
    end
    until i = noeia2;

    { do whatever else }
    end.[/code]
  • Thanks for all your responses, i was too lazy to post yesterday evening, so i post my very own version now. read the last two entries 5 minutes before.

    Here it is (with an example):
  • Thanks for all your responses, i was too lazy to post yesterday evening, so i post my very own version now. read the last two entries 5 minutes before.

    Here it is (with an example):

    [code]PROGRAM MergeArrays;

    USES WinCrt;

    CONST
    lengthInput1 = 5;
    lengthInput2 = 14;
    maxLength = 19;

    PROCEDURE Merge(array1, array2: ARRAY OF INTEGER; length1, length2: INTEGER;
    VAR result: ARRAY OF INTEGER; VAR resultLength: INTEGER);
    VAR
    i, j, cnt : INTEGER;
    checkArr : ARRAY [0..maxLength] OF BOOLEAN;
    resultCount : INTEGER;

    BEGIN
    (* Initialize Variables *)
    i := 0;
    j := 0;
    IF (length1 = 0) AND (length2 > 0) THEN BEGIN
    resultCount := 1;
    cnt := 1
    END
    ELSE BEGIN
    resultCount := -1;
    cnt := length1;
    END;

    (* Initialize the check-Array with FALSE
    otherwise it seems it gets filled with
    random values at start *)
    FOR i := 0 TO maxLength DO BEGIN
    checkArr[i] := FALSE;
    END; (* END FOR (checkArr Initialization) *)

    FOR i := 0 TO length1 DO BEGIN
    result[i] := array1[i];
    IF length1 > 0 THEN BEGIN (* IF length1 > 0 THEN the resultLength *)
    Inc(resultCount); (* should be increased by one. IF Not, *)
    END; (* The Value will stay 0 for the Output *)
    END;

    FOR i := 0 TO length2 DO BEGIN
    FOR j := 0 TO length1 DO BEGIN
    IF array2[i] = array1[j] THEN BEGIN
    checkArr[i] := TRUE;
    END; (* END IF (checkArr filling) *)
    END; (* END FOR (j) *)
    IF checkArr[i] = FALSE THEN BEGIN
    result[i+cnt] := array2[i];
    Inc(resultCount)
    END
    ELSE BEGIN
    Dec(cnt);
    END; (* END IF (checkArr questioning) *)
    END; (* END FOR (i) *)

    resultLength := resultCount;

    END;

    VAR
    arr1 : ARRAY [0..lengthInput1] OF INTEGER;
    arr2 : ARRAY [0..lengthInput2] OF INTEGER;
    resultArr : ARRAY [0..maxLength] OF INTEGER;
    lengthCalc : INTEGER;
    i : INTEGER;

    BEGIN
    i := 0;
    lengthCalc := 0;

    FOR i := 0 TO lengthInput1-1 DO BEGIN
    arr1[i] := i;
    END;


    FOR i := 0 TO lengthInput2-1 DO BEGIN
    arr2[i] := i;
    END;

    Write('array1-Data: ');
    FOR i := 0 TO lengthInput1-1 DO BEGIN
    Write(arr1[i],' ');
    END;
    WriteLn;
    Write('array2-Data: ');;
    FOR i := 0 TO lengthInput2-1 DO BEGIN
    Write(arr2[i],' ');
    END;

    WriteLn;
    WriteLn;

    Merge(arr1, arr2, lengthInput1, lengthInput2, resultArr, lengthCalc);

    WriteLn('Length of merged Array: ',lengthCalc);
    WriteLn;

    FOR i := 0 TO lengthCalc-1 DO BEGIN
    WriteLn(resultArr[i]);
    END;


    END.
    [/code]
  • I appreciate the concern which is been rose. The things need to be sorted out because it is about the individual but it can be with everyone. The initiative taken for the concern is very serious and need an attention of every one. This is the concern which exists in the society and needs to be eliminated from the society as soon as possible. There is nothing called a free lunch is this world. If anything needed to be resolved then initiated need a support to be sorted out.
    ====================================
    [link=http://www.motorhomeinsurance.mobi]Motorhome Insurance Uk[/link]
  • : I appreciate the concern which is been rose. The things need to be
    : sorted out because it is about the individual but it can be with
    : everyone. The initiative taken for the concern is very serious and
    : need an attention of every one. This is the concern which exists in
    : the society and needs to be eliminated from the society as soon as
    : possible. There is nothing called a free lunch is this world. If
    : anything needed to be resolved then initiated need a support to be
    : sorted out.
    :

    Ok, ok, got your point..., but at the end of the day are still things which could be considered "free lunch" on the net and even in real life...apart from those sites where you pay to get your problem answered.
    The whole point [u]here[/u] is the info sharing and helping fellow programmers, there is almost no way of knowing if a question asked here is for a school assignment or some other stuff. So, IMO the reply for any given post is rests upon the sole discretion of the person who takes the time and/or effort to do so. I don't necessarily ask what the background of a question is, that's not my problem, but will continue to help out in the future if I can and feel like...my 2 cents.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories