Algorithm: efficient way to remove duplicate integers from an array

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

    In Java I would solve it like this. Don't know how to write this in C.

       int length = array.length;
       for (int i = 0; i < length; i++) 
       {
          for (int j = i + 1; j < length; j++) 
          {
             if (array[i] == array[j]) 
             {
                int k, j;
                for (k = j + 1, l = j; k < length; k++, l++) 
                {
                   if (array[k] != array[i]) 
                   {
                      array[l] = array[k];
                   }
                   else
                   {
                      l--;
                   }
                }
                length = l;
             }
          }
       }
    

提交回复
热议问题