LeetCode

Problem Statement

Given a binary array, find the maximum number of consecutive 1s in this array.

Example:

Given a binary array, find the maximum number of consecutive 1s in this array.

Solution

func findMaxConsecutiveOnes(nums []int) int {
	max, counter := 0, 0
	for _, v := range nums {
		if v == 1 {
			counter++
			continue
		}
		if counter > max {
			max = counter
		}
		counter = 0
	}
	if counter > max {
		max = counter
	}
	return max
}