Algorithm: efficient way to remove duplicate integers from an array

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

    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] ;                         
         }
    }
    

提交回复
热议问题