获取数组中最大连续出现的值

时间:2022-11-27 22:57:13

Is there a more elegant way to achieve this below:

是否有更优雅的方式来实现以下目标:

Input:

array = [1, 1, 1, 0, 0, 1, 1, 1, 1, 0]

Output:

4

My algo:

streak = 0
max_streak = 0

arr.each do |n|
  if n == 1
    streak += 1
  else
    max_streak = streak if streak > max_streak
    streak = 0
  end
end

puts max_streak

4 个解决方案

#1


6  

Similar to w0lf's answer, but skipping elements by returning nil from chunk:

类似于w0lf的答案,但是通过从chunk返回nil来跳过元素:

array.chunk { |x| x == 1 || nil }.map { |_, x| x.size }.max

#2


6  

Edit: Another way to do this (that is less generic than Stefan's answer since you would have to flatten and split again if there was another number other than 0 and 1 in there, but easier to use in this case):

编辑:另一种方法(比Stefan的答案更不通用,因为如果那里有另外的数字而不是0和1,你必须再次展平和拆分,但在这种情况下更容易使用):

array.split(0).max.count

You can use:

您可以使用:

array.chunk { |n| n }.select { |a| a.include?(1) }.map { |y, ys| ys.count}.max

ref: Count sequential occurrences of element in ruby array

ref:计算ruby数组中元素的连续出现次数

#3


5  

You can use Enumerable#chunk:

您可以使用Enumerable#chunk:

p array.chunk{|x| x}.select{|x, xs| x == 1}.map{|x, xs| xs.size }.max

This is more concise, but if performance was important, I'd use your approach.

这更简洁,但如果性能很重要,我会使用您的方法。


Edit: If you're in Ruby 2.2.2, you can also use the new Enumerable#slice_when method (assuming your input array consists of only 0s and 1s):

编辑:如果你在Ruby 2.2.2中,你也可以使用新的Enumerable#slice_when方法(假设你的输入数组只包含0和1):

array.slice_when{|x,y| x < y }.map{|slice| slice.count 1 }.max

#4


0  

How about

array = [1, 1, 1, 0, 0, 1, 1, 1, 1, 0]

array.split(0).group_by(&:size).max.first #=> 4

array.split(0).group_by(&:size).max.first#=> 4

The only bad thing - split(0)

唯一的坏事 - 分裂(0)

Note: This only works with rails's ActiveSupport(extends Array with #split)

注意:这仅适用于rails的ActiveSupport(使用#split扩展数组)

For ruby-only implementation

仅用于ruby实现

array.join.split("0").group_by(&:size).max.first #=> 4

#1


6  

Similar to w0lf's answer, but skipping elements by returning nil from chunk:

类似于w0lf的答案,但是通过从chunk返回nil来跳过元素:

array.chunk { |x| x == 1 || nil }.map { |_, x| x.size }.max

#2


6  

Edit: Another way to do this (that is less generic than Stefan's answer since you would have to flatten and split again if there was another number other than 0 and 1 in there, but easier to use in this case):

编辑:另一种方法(比Stefan的答案更不通用,因为如果那里有另外的数字而不是0和1,你必须再次展平和拆分,但在这种情况下更容易使用):

array.split(0).max.count

You can use:

您可以使用:

array.chunk { |n| n }.select { |a| a.include?(1) }.map { |y, ys| ys.count}.max

ref: Count sequential occurrences of element in ruby array

ref:计算ruby数组中元素的连续出现次数

#3


5  

You can use Enumerable#chunk:

您可以使用Enumerable#chunk:

p array.chunk{|x| x}.select{|x, xs| x == 1}.map{|x, xs| xs.size }.max

This is more concise, but if performance was important, I'd use your approach.

这更简洁,但如果性能很重要,我会使用您的方法。


Edit: If you're in Ruby 2.2.2, you can also use the new Enumerable#slice_when method (assuming your input array consists of only 0s and 1s):

编辑:如果你在Ruby 2.2.2中,你也可以使用新的Enumerable#slice_when方法(假设你的输入数组只包含0和1):

array.slice_when{|x,y| x < y }.map{|slice| slice.count 1 }.max

#4


0  

How about

array = [1, 1, 1, 0, 0, 1, 1, 1, 1, 0]

array.split(0).group_by(&:size).max.first #=> 4

array.split(0).group_by(&:size).max.first#=> 4

The only bad thing - split(0)

唯一的坏事 - 分裂(0)

Note: This only works with rails's ActiveSupport(extends Array with #split)

注意:这仅适用于rails的ActiveSupport(使用#split扩展数组)

For ruby-only implementation

仅用于ruby实现

array.join.split("0").group_by(&:size).max.first #=> 4