Is there a more elegant way to achieve this below:
Input:
array = [1, 1, 1, 0, 0, 1, 1, 1, 1, 0]
Output:
4
---------------------------------------------------
Similar to w0lf's answer, but skipping elements by returning nil
from chunk
:
array.chunk { |x| x == 1 || nil }.map { |_, x| x.size }.max
//
You can use:
array.chunk { |n| n }.select { |a| a.include?(1) }.map { |y, ys| ys.count}.max
//
You can use Enumerable#chunk
:
array.join.split("0").group_by(&:size).max.first
p array.chunk{|x| x}.select{|x, xs| x == 1}.map{|x, xs| xs.size }.max
//