Remove duplicates from Array without using Hash Table

后端 未结 7 1618
终归单人心
终归单人心 2020-12-15 14:37

i have an array which might contain duplicate elements(more than two duplicates of an element). I wonder if it\'s possible to find and remove the duplicates in the array:

7条回答
  •  有刺的猬
    2020-12-15 15:17

    Since it's an interview question it is usually expected by the interviewer to be asked precisions about the problem.

    With no alternative storage allowed (that is O(1) storage allowed in that you'll probably use some counters / pointers), it seems obvious that a destructive operation is expected, it might be worth pointing it out to the interviewer.

    Now the real question is: do you want to preserve the relative order of the elements ? ie is this operation supposed to be stable ?

    Stability hugely impact the available algorithms (and thus the complexity).

    The most obvious choice is to list Sorting Algorithms, after all, once the data is sorted, it's pretty easy to get unique elements.

    But if you want stability, you cannot actually sort the data (since you could not get the "right" order back) and thus I wonder if it solvable in less than O(N**2) if stability is involved.

提交回复
热议问题