Best algorithm for delete duplicates in array of strings

前端 未结 7 2034
一个人的身影
一个人的身影 2021-02-09 02:44

Today at school the teacher asked us to implement a duplicate-deletion algorithm. It\'s not that difficult, and everyone came up with the following solution (pseudocode):

<
7条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-09 03:13

    The easiest solution will be to simply sort the array (takes O(n log n) with standard implementation if you may use them. otherwise consider making an easy randomized quicksort (code is even on wikipedia)).

    Afterwards scan it for one additional time. During that scan simple eliminate consecutive identical elements.

    If you want to do it in O(n), you can also use a HashSet with elements you have already seen. Just iterate once over your array, for each element check if it is in your HashSet.

    If it isn't in there, add it. If it is in there, remove it from the array.

    Note, that this will take some additional memory and the hashing will have a constant factor that contributes to your runtime. Althought the time complexity is better, the practical runtime will only be onyl be faster once you exceed a certain array size

提交回复
热议问题