Saturday, 28 September 2013

Removing duplicate values from SplDoublyLinkedList

Removing duplicate values from SplDoublyLinkedList

Is there a method to remove duplicate values from a list? I was trying to
create my own method, but it takes quite a lot time depending on how much
there is duplicate values:
$arr = new SplDoublyLinkedList();
for ($i = 0; $i < count($arr); $i++) {
for ($j = $i; $j < count($arr); $j++) {
if ($arr[$i] == $arr[$j] && $i != $j) {
$arr->offsetUnset($j);
$j -= 1;
}
}
}
E.g. if there is a 4000 items and about 1000 unique items on a list, it
takes about 7 seconds to remove these items.

No comments:

Post a Comment