开发者

Removing duplicates from two comma separated strings

开发者 https://www.devze.com 2023-03-18 06:08 出处:网络
I\'m comparing two 开发者_运维技巧comma-separated strings and completely removing duplicates. That is, if an item exists in both strings, it will not exist in the result.

I'm comparing two 开发者_运维技巧comma-separated strings and completely removing duplicates. That is, if an item exists in both strings, it will not exist in the result.

For example, the result of comparing cat,dog,alligator and alligator,parakeet will be cat,dog,parakeet.

The solution I'm using involves converting the strings to arrays to compare.

$result = array_merge( array_diff($old, $new), array_diff($new, $old) );

The above works, but I'm wondering if there is a more efficient way (regular expressions, string comparisons) to do the comparison without converting the strings to arrays first. Or is the performance hit for the above solution really not that bad?

Thanks


Array operations are definitely going to be efficient enough, unless you're trying to squeeze a few seconds out of a million-item calculation. Your solution is the simplest, and performs perfectly well, so is therefore the best.


Interesting problem. Optionally, there are regular expressions that could do the work. But arrays are much faster in this case. To minimize function calls you I'd write something like this:

$str1 = "cat,dog,alligator";
$str2 = "alligator,parakeet";

$result = array_unique((array) explode(',', $str1 . ',' . $str2));


You can look at this as Set Math. In your case you want the union minus the intersection (i.e. symmetric difference).

For example:

$result = array_diff(array_merge($s1, $s2), array_intersection($s1, $s2));

Although you could loop over each item and perform the logic yourself (Such as the answer from TheHorse), in the end, I believe PHP's native array functions are going to be more performant and offer more flexibility (e.g. multiple sets).

With that said, your solution works and is the PHP equivalent representation of the symmetric difference. So why change it...


For big number of element's good solution is:

<?php
  $temp = sort(array_merge($old, $new)); /* n*log(n); */
  $res = array();
  for ($i = 0; $i < count($temp); $i++)
  {
     if ((count($res) == 0) || ($res[count($res) - 1] !== $temp[$i]))
       $res.append($temp[$i]);
  } 
?>

P.S. You can optimize that.

0

精彩评论

暂无评论...
验证码 换一张
取 消