Algorithm: efficient way to remove duplicate integers from an array

前端 未结 30 2205
离开以前
离开以前 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

    If you are allowed to use C++, a call to std::sort followed by a call to std::unique will give you the answer. The time complexity is O(N log N) for the sort and O(N) for the unique traversal.

    And if C++ is off the table there isn't anything that keeps these same algorithms from being written in C.

    0 讨论(0)
  • 2020-11-22 16:23

    Here is my solution.

    ///// find duplicates in an array and remove them
    
    void unique(int* input, int n)
    {
         merge_sort(input, 0, n) ;
    
         int prev = 0  ;
    
         for(int i = 1 ; i < n ; i++)
         {
              if(input[i] != input[prev])
                   if(prev < i-1)
                       input[prev++] = input[i] ;                         
         }
    }
    
    0 讨论(0)
  • 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;
    
    0 讨论(0)
  • 2020-11-22 16:24

    One more efficient implementation

    int i, j;
    
    /* new length of modified array */
    int NewLength = 1;
    
    for(i=1; i< Length; i++){
    
       for(j=0; j< NewLength ; j++)
       {
    
          if(array[i] == array[j])
          break;
       }
    
       /* if none of the values in index[0..j] of array is not same as array[i],
          then copy the current value to corresponding new position in array */
    
      if (j==NewLength )
          array[NewLength++] = array[i];
    }
    

    In this implementation there is no need for sorting the array. Also if a duplicate element is found, there is no need for shifting all elements after this by one position.

    The output of this code is array[] with size NewLength

    Here we are starting from the 2nd elemt in array and comparing it with all the elements in array up to this array. We are holding an extra index variable 'NewLength' for modifying the input array. NewLength variabel is initialized to 0.

    Element in array[1] will be compared with array[0]. If they are different, then value in array[NewLength] will be modified with array[1] and increment NewLength. If they are same, NewLength will not be modified.

    So if we have an array [1 2 1 3 1], then

    In First pass of 'j' loop, array[1] (2) will be compared with array0, then 2 will be written to array[NewLength] = array[1] so array will be [1 2] since NewLength = 2

    In second pass of 'j' loop, array[2] (1) will be compared with array0 and array1. Here since array[2] (1) and array0 are same loop will break here. so array will be [1 2] since NewLength = 2

    and so on

    0 讨论(0)
  • 2020-11-22 16:24

    In JAVA,

        Integer[] arrayInteger = {1,2,3,4,3,2,4,6,7,8,9,9,10};
    
        String value ="";
    
        for(Integer i:arrayInteger)
        {
            if(!value.contains(Integer.toString(i))){
                value +=Integer.toString(i)+",";
            }
    
        }
    
        String[] arraySplitToString = value.split(",");
        Integer[] arrayIntResult = new Integer[arraySplitToString.length];
        for(int i = 0 ; i < arraySplitToString.length ; i++){
            arrayIntResult[i] = Integer.parseInt(arraySplitToString[i]);
        }
    

    output: { 1, 2, 3, 4, 6, 7, 8, 9, 10}

    hope this will help

    0 讨论(0)
  • 2020-11-22 16:25

    If you are looking for the superior O-notation, then sorting the array with an O(n log n) sort then doing a O(n) traversal may be the best route. Without sorting, you are looking at O(n^2).

    Edit: if you are just doing integers, then you can also do radix sort to get O(n).

    0 讨论(0)
提交回复
热议问题