Contiguous Array

You are given a binary array nums (every element is 0 or 1). Return the length of the longest contiguous subarray that contains the same number of 0s and 1s.

If no such subarray exists, return 0.

Constraints

  • 1 <= nums.length <= 10^5
  • nums[i] is 0 or 1.

Examples

Example 1

Input: nums = [0,0,1,0,1,1,0]
Output: 6

Indices 1 to 6 hold 0, 1, 0, 1, 1, 0: three 0s and three 1s, length 6.

Example 2

Input: nums = [1,1,1]
Output: 0

There are no 0s, so no balanced subarray exists: return 0.

Example 3

Input: nums = [0,1]
Output: 2

The whole array has one 0 and one 1.

JavaScript you'll use: Map and Set Arrays: create, read, update

/**
 * @param {number[]} nums
 * @return {number}
 */
function findMaxLength(nums) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.