Algorithm: efficient way to remove duplicate integers from an array

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

    How about:

    void rmdup(int *array, int length)
    {
        int *current , *end = array + length - 1;
    
        for ( current = array + 1; array < end; array++, current = array + 1 )
        {
            while ( current <= end )
            {
                if ( *current == *array )
                {
                    *current = *end--;
                }
                else
                {
                    current++;
                }
            }
        }
    }
    

    Should be O(n^2) or less.

提交回复
热议问题