关于大o:array_unique PHP的动作数

Number of actions of array_unique PHP

有人知道array_unique()Big O吗?

我还没有遍历源代码,但是我可以想象它遍历每个值并检查是否在数组中,它是否为O(n^2)?这是正确的吗?

谢谢


它是O(nlogn),因为它使用排序而不是您的O(n^2)扫描。

Note that keys are preserved. array_unique() sorts the values treated as string at first, then will keep the first key encountered for every value, and ignore all following keys. It does not mean that the key of the first related value from the unsorted array will be kept.

引自http://php.net/manual/en/function.array-unique.php

编辑:记住,请谷歌阅读它,查看手册,检查是否存在问题,然后再提出。