Algorithm: efficient way to remove duplicate integers from an array

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

    An array should obviously be "traversed" right-to-left to avoid unneccessary copying of values back and forth.

    If you have unlimited memory, you can allocate a bit array for sizeof(type-of-element-in-array) / 8 bytes to have each bit signify whether you've already encountered corresponding value or not.

    If you don't, I can't think of anything better than traversing an array and comparing each value with values that follow it and then if duplicate is found, remove these values altogether. This is somewhere near O(n^2) (or O((n^2-n)/2)).

    IBM has an article on kinda close subject.

提交回复
热议问题