简体   繁体   English

array_unique或将值存储为数组中的键

[英]array_unique or store values as keys in array

I am populating an array with strings that may contain duplicates. 我正在用可能包含重复项的字符串填充数组。 It's a large array. 这是一个很大的阵列。

Is it better I store each string as the key of the array itself, thus handling duplicates automatically 我是否最好将每个字符串存储为数组本身的键,从而自动处理重复项

eg array['test'] = true 例如 array['test'] = true

Or is it more efficient to store them all in the array as 或者将它们全部存储为数组更有效

eg array[] = 'test'; 例如 array[] = 'test';

and then do an array_unique? 然后做一个array_unique吗?

If you store each string as the key of the array itself it will take N time for each element to be inserted for an execution time of O(N). 如果将每个字符串存储为数组本身的键,则每个元素插入将花费N时间,执行时间为O(N)。

If you do it with the traditional insert and then check with array_unique it would be > O(N). 如果使用传统的插入方法执行此操作,然后使用array_unique进行检查,则该值应为> O(N)。

Essentially it would take more time for the second method because you would be iterating the array multiple times instead of once. 本质上,第二种方法将花费更多时间,因为您将多次而不是一次迭代数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM