What's the best way to remove duplicates from a string in PHP (or any language)?

好久不见. 提交于 2019-12-08 12:25:35

问题


I am looking for the best known algorithm for removing duplicates from a string. I can think of numerous ways of doing this, but I am looking for a solution that is known for being particularly efficient.

Let's say you have the following strings:

  • Lorem Ipsum Lorem Ipsum
  • Lorem Lorem Lorem
  • Lorem Ipsum Dolor Lorem Ipsum Dolor Lorem Ipsum Dolor

I would expect this algorithm to output for each (respectively):

  • Lorem Ipsum
  • Lorem
  • Lorem Ipsum Dolor

Note, I am doing this in PHP, in case anybody is aware of any built in PHP functions that can help with this.

Thanks!


回答1:


$arr = explode( " " , $string );
$arr = array_unique( $arr );
$string = implode(" " , $arr);



回答2:


Dunno about efficiency, but maybe this can do:

$str = implode(" ", array_unique(explode(" ", $str)));



回答3:


$words = array_unique(explode(' ',$text));
echo implode(' ',$words);

if you want to make it better you can use preg_split with \s\W for exploding words




回答4:


Best way of doing it:

  1. Sort the words inside string
  2. Remove duplicates by iterating the sorted words

Other possibility is using a set construction if your language supports it.




回答5:


You can try below code for removing duplicate code from any sentence

$arr = explode(" " , $string);
$arr = preg_replace('/(\w{2,})(?=.*?\\1)\W*/', '', $arr);
$string = implode(" " , $arr);


来源:https://stackoverflow.com/questions/5331056/whats-the-best-way-to-remove-duplicates-from-a-string-in-php-or-any-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!