如何从一个数组中删除重复项组中、个数

2023-09-11 06:48:34 作者:重生爱人i

可能重复:   阵列删除重复的元素

这是一个家庭作业的问题。我认为最好的办法是修改后的归并排序,其中,修改后的合并只是不从两个复制重复的元素输入数组的结果数组。的复杂度为O(N * logN)的(完全是正规归并排序)。是否有意义 ?有没有更好的办法呢?

This is a homework question. I think the best way is a modified merge sort, where the modified merge just does not copy duplicate elements from the two input arrays to the result array. The complexity is O(N*logN) (exactly as the regular merge sort). Does it make sense ? Are there any better solutions ?

推荐答案

如果你不需要进行排序,只能通过使用HashSet的(或您的语言相当)删除重复它可以在O完成(N)的时间。

If you do not need to sort, only remove the duplicates it can be done in O(N) time by using a HashSet (or your language equivalent).

 
精彩推荐
图片推荐