How do I delete the earlier array value if similar values exist? Here’s the code I use:
def address_geo
arr = []
arr << do if do
arr << re if re
arr << me if me
arr << fa if fa
arr << so if so
arr << la if la
arr.reject{|y|y==''}.join(' ')
end
Given the following values
do = 'I'
re = 'am'
me = 'a'
fa = 'good'
so = 'good'
la = 'boy'
The above method would yield:
I am a good good boy
How should I write the array merge to reject fa and just take so to yield:
I am a good boy
Many thanks!
You can use Array#uniq
As per @tokland’s suggestion, if you wanted to remove only consecutive duplicates, this would work (and support ruby 1.8). By building a new array using
injectwe can filter out each string that is eitherempty?, or the same as the previous string.