Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove duplicate substrings from a string

Tags:

string

ruby

str = "hi ram hi shyam hi jhon"

I want something like:

"ram hi shyam hi jhon"
"ram shyam hi jhon"
like image 521
Vish Avatar asked Oct 23 '25 00:10

Vish


1 Answers

I assume you want to remove duplicate occurrences of all words, not just "hi". Here are two ways of doing that.

1 Use String#reverse, Array#reverse and Array#uniq

str = "hi shyam ram hi         shyam hi jhon"

str.split.reverse.uniq.reverse.join(' ')
  #=> "ram shyam hi jhon"

The doc for uniq states: "self is traversed in order, and the first occurrence is kept."

2 Use a regular expression

r = /
    \b      # match a word break
    (\w+)   # match a word in capture group 1
    \s      # match a trailing space
    (?=     # begin a positive lookahead
      .*    # match any number of characters
      \s    # match a space
      \1    # match the contents of capture group 1
      \b    # match a word break
    )       # end the positive lookahead
    /x      # free-spacing regex definition mode

str.gsub(r, '')
  #=> "ram         shyam hi jhon"

To remove the extra spaces change \s to \s+ in the third line of the regex definition.

like image 123
Cary Swoveland Avatar answered Oct 25 '25 17:10

Cary Swoveland