且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除字符串数组中重复项的***算法

更新时间:2023-01-12 22:18:01

最简单的解决方案是简单地对数组进行排序(如果你可以使用它们,使用标准实现需要 O(n log n).否则考虑做一个简单的随机化快速排序(代码甚至在***上)).

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.

如果你想在 O(n) 中完成,你也可以使用 HashSet 和你已经见过的元素.只需遍历您的数组一次,检查每个元素是否在您的 HashSet 中.

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