一维数组中有两个数相同,怎么用最短的时间找出来啊!
别让我一个一个比较啊!

解决方案 »

  1.   

    var aa:array[1..100] of integer;ba:array[1..100] of integer;i,n:integer;
    begin
     ..............//Initialize your aa and ba for i:=1 to 100 do
        for n:=1 to 100 do
           if aa[i]=ba[n] then showmessage(format('Found in aa[%d] and ba[%d]',[i,n]));
      end;
      

  2.   

    to:Eastunfail
      除了这样挨个比较就没有好办法了吗?
      

  3.   

    不一个一个比较怎么比较呢?不过楼上写的两个一维数组。
    var
      aa: array [1..100] of integer;
      i, j: integer;
    begin
      // init array aa
      for i := 1 to 100 do
        for j := 100 downto i + 1 do
          if aa[i] = aa[j] then ShowMessage(format('Found in aa[%d] and ba[%d]',[i,n]))
    end;
      

  4.   

    var 
      aa:array[1..100] of integer;ba:array[1..100] of integer;i,n:integer;
      bFound: Boolean;
    begin
     ..............//Initialize your aa and ba  bFound := False;
      for I := Low(aa) to High(aa) do
        for J := Low(aa) to High(aa) do
        begin
          if I = J then continue;
          if aa[I] = aa[J] then 
          begin
            bFound := True;
            Break;
          end;
        end;
        if bFound then Break;
      end;
      if bFound then  ShowMessage(format('Found in aa[%d] and ba[%d]',[i,n]))
      else  ShowMessage('....');
    end;
      

  5.   

    如果这个数组是一个Integer的数组,而且所有的元素都小于255,那么还是有一个简单一点的方法的。首先要声明一下,我只是最近才开始学 Delphi,所以下面的代码可能会有一些语法问题,所以它只是给你一个思路,也就是说你应该把它当作伪代码。var
      IntegerArray : Integer[1..100] of Integer;
      i : Integer;
      IntegerSet : Set of 0..255;begin
      i:=1;
      repeat 
        IntegerSet := Integer + [IntegerArray[i]];
        inc(i);
      until (IntegerArray[i] in IntegerSet) or (i > High(IntegerArray));
      if i < high(IntegerArray) then
        writeln(IntegerArray[i])
      else
        writeln("All Elements in Array are unique!")
    end;
      

  6.   

    while(i++<j--)
    {
      if a[i]==a[j] then break;
    }
      

  7.   

    错了,用惯了c 
    i:=0;
    j:=元素个数;
    while i<j do
    begin
      if a[i]=a[j] then break;
      inc(i);
      dec(j);
    end;
      

  8.   

    不好意思,上面的回答没有深思熟虑,重来
    procedure TForm1.Button1Click(Sender: TObject);
    var
      a:array[0..15]of integer;
      i,j:integer;
    begin
      for i:=0 to 7 do
       a[i]:=i;
      for i:=15 downto 7 do
        a[i]:=i-7;
      for i:=0 to 15 do
        ListBox2.Items.Append(inttostr(a[i]));
    for i:=0 to high(a) do
      for j:=high(a) downto 0 do
      begin
       if i=j then continue;
       if a[i]=a[j] then ListBox1.Items.Append(inttostr(a[i]));
      end;
    end;
      

  9.   

    还有一个算法,其前提是数组所有的元素必须都是正整数var 
    SourceArray: array[1..100] of Integer;
    IntArray : array of Integer;
    max : Integer;
    i : Integer;begin
       max  := 0;
       for i:=1 to high(SourceArray) do
       begin
          if max < SourceArray[i] then max := SourceArray[i];
          inc(i);
       end;
       SetLength(IntArray, max+1);
       for i:=1 to high(SourceArray) do
       begin
          inc(IntArray[SourceArray[i]]);
       end;
       i:= 0;
       while i<= high(IntArray) do
       begin
          if IntArray[i] >= 2 then writeln(""+ IntArray[i]);
          inc(i);
       end;
    end;这个算法要作三次循环。第一次,找出数组中最大的数。然后建立一个新的,容量与这个最大的数相同的数组。第二次遍历源数组,每次找到一个数字就让新建的那个数组中的,下标为这个数字的元素加一。第三次遍历新建数组,找出新建数组中所有大于等于2的元素,然后可以说这些数字重复了超过两遍。
      

  10.   

    不好意思,上面代码里面的for语法不对,现在作更正var
    SourceArray: array[1..100] of Integer;
    IntArray : array of Integer;
    max : Integer;
    i : Integer;begin
    max := 0;
    for i:=1 to high(SourceArray) do
    if max < SourceArray[i] then max := SourceArray[i];
    SetLength(IntArray, max+1);
    for i:=1 to high(SourceArray) do inc(IntArray[SourceArray[i]]);
    i:= 0;
    for i:= 0 to high(IntArray) do
    if IntArray[i] >= 2 then writeln(""+ IntArray[i]);
    end;
      

  11.   

    当然有更好的算法,最差情况下只要扫描一遍,大多情况下连一遍都不需要:
    var
    s : string;
    i : integer;
    begin
    s := '';
    for i := low(aarray) to high(aarray) do begin
        if Pos(InttoStr(aarray[i],s) > 0 then begin
           ShowMessage('Found!');
           Break;
        end else
           s := s + InttoStr(aarray[i])+' '
    end;
      

  12.   

    楼楼上的,pos也是扫描哎!这样不会提高效率啦。
      

  13.   

    type
       PItem = ^Item;
       TItem = record
          Index: Integer;
          Next: PItem;
       end;var
       arrMap: array [0..255] of PItem;procedure InitialMap;
    begin
       FillChar(arrMap, SizeOf(arrMap), 0);
    end;procedure FreeMap;
    begin
       // ??? ... ...
    end;function Add(AIndex, AValue: Integer; AList: array of Integer): Integer;
    var
       intItem: Integer;
       pCurrent: PItem;
    begin
       intItem := AValue mod 256;
       pCurrent := arrMap[intItem];
       result := -1;
       while pCurrent <> nil do
       begin
          if AList[pCurrent^.Index] = AValue then
          begin
             result := pCurrent^.Index;
             Break;
          end;
          pCurrent := pCurrent^.Next;
       end;   // No Exist, Add to Map
       if result = -1 then
       begin
          New(pCurrent);
          pCurrent^.Index := AIndex;
          pCurrent^.Next := arrMap[intItem];
          arrMap[intItem] := pCurrent;
       end;
    end;function FindEqualIndex(AList: array of Integer; var A, B: Integer): Boolean;
    var
       intNo: Integer;
    begin
       A := -1;
       B := -1;
       InitialMap;
       result := False;
       try
          for intNo := 0 to High(AList) do
          begin
             A := Add(intNo, AList[intNo], AList);
             if A >= 0 then
             begin
                B := intNo;
                result := True;
                Break;
             end;
          end;
       finally
          FreeMap;
       end;
    end;
      

  14.   

    kyee(浪子阿鹏) :
    pos是内部函数,用汇编写的,所以速度是最快的,肯定比你用高级语言写的任何同功能的函数速度要快.
      

  15.   

    谬论,汇编写一定就快?!Pos函数也是经过顺序查找,你的算法复杂度为:n(n+1)/4,最坏时为: n(n+1)/2,请仔细考虑一下!!!我的算法还有提高的余地,即不用 mod 256,而用其他方法如奇偶位平方后再 mod 256,或其他方法,即尽量把值分散。256 可以改变更大的数字,如 shr 16 等等。
    内存也可以预先根据项数 GetMem,自己管理相对地址即可,这样效率会更高。
      

  16.   

    cld
    lea esi, Source   // 假设 Source 数据尺寸为 $7FFFFFFF
    lea edi, Dest
    mov ecx, $7FFFFFFF
    rep movsb以上虽然指令简单,但是所需时间比mov al, Source[0]
    mov Dest[0], al
    ...
    mov al, Source[999]
    mov Dest[999], al要大得多,我只想说明 Pos 函数虽然很快,但其时间复杂度是存在的,不可逃避的现实。
      

  17.   

    算法的讨论应该只使用语言的提供的保留字,毕竟是理论上的内容,应不涉及处理器的优劣.
    尽量不要使用borland给咱们的函数--那不如考虑并行处理,这样可以成倍提高算法效率.
      

  18.   

    sunware():
       如 a[0] := 123456; a[1] := 3456; 
       用你的算法回认为两者相同