PHP performance question: is it faster to leave duplicates in an array that will be executed or array_unique will be executed?

I have code that adds values ​​to an array. The array will later search in another part of my code. The values ​​that are added to the array are not necessarily unique, so duplicate values ​​can be found as a result of the search. From a technical point of view, even if there are duplicates present in the array, my code works fine and I can find the value. I just want to know if the value is in the array that is being searched and does not care if it is in the array 1 time or 10,000 times.

My question is whether he prefers (for performance and / or style) to execute array_unique () in my array, which is searched before doing the search.

So, for example, suppose I want to search for such an array:

$searchMe = Array("dog", "cat", "mouse", "dog", "dog", "dog");

Please note that the “dog” is present 4 times. If I want to find the value "dog", in this array it will work fine, and I can say that it is present. As mentioned above, I don’t care how many times he is present, I just want to know if he is present at all.

So, should I do this first before searching and then doing a search on a nullified array?

$searchMe_cleaned = array_unique($searchMe);

Ie, will it be faster than just finding an array with duplicates?

, , , .

!

+3
3

, array_unique , in_array, , .

array_flip ( ), isset array_key_exists, , in_array, .

+6

array_unique sqrt(n) , in_array. ,

PS: ,

($ [$ ])

, in_array,

+1

, :

, array_keys(array_flip($array)); , array_unique(); , 80% 100 , 95% 1000 99% 10 000+.

, () , , array_keys(array_flip($array)); , .

0

Source: https://habr.com/ru/post/1792947/


All Articles