如何从一个数组中删除重复项 [英] How to remove duplicates from an array
本文介绍了如何从一个数组中删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
阵列删除重复的元素
这是一个家庭作业的问题。我认为最好的办法是修改后的归并排序
,其中,修改后的合并
只是不从两个复制重复的元素输入数组的结果数组。的复杂度为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).
这篇关于如何从一个数组中删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文