Algorithm: efficient way to remove duplicate integers from an array

前端 未结 30 2237
离开以前
离开以前 2020-11-22 16:03

I got this problem from an interview with Microsoft.

Given an array of random integers, write an algorithm in C that removes duplicated numbers an

30条回答
  •  无人及你
    2020-11-22 16:23

    After review the problem, here is my delphi way, that may help

    var
    A: Array of Integer;
    I,J,C,K, P: Integer;
    begin
    C:=10;
    SetLength(A,10);
    A[0]:=1; A[1]:=4; A[2]:=2; A[3]:=6; A[4]:=3; A[5]:=4;
    A[6]:=3; A[7]:=4; A[8]:=2; A[9]:=5;
    
    for I := 0 to C-1 do
    begin
      for J := I+1 to C-1 do
        if A[I]=A[J] then
        begin
          for K := C-1 Downto J do
            if A[J]<>A[k] then
            begin
              P:=A[K];
              A[K]:=0;
              A[J]:=P;
              C:=K;
              break;
            end
            else
            begin
              A[K]:=0;
              C:=K;
            end;
        end;
    end;
    
    //tructate array
    setlength(A,C);
    end;
    

提交回复
热议问题