Remove adjacent identical elements in a Ruby Array?

前端 未结 6 1572
醉酒成梦
醉酒成梦 2021-02-14 12:55

Ruby 1.8.6

I have an array containing numerical values. I want to reduce it such that sequences of the same value are reduced to a single instance of that value.

6条回答
  •  梦谈多话
    2021-02-14 13:52

    I can think only of this

    a.each_with_index{|item,i| a[i] = nil if a[i] == a[i+1] }.compact
    

    but it is more or less the same.

提交回复
热议问题