Krissy
Krissy

Reputation: 19

Return maximum length of the sequence of same items within array

I need to write a function called countZeroes, which accepts an array of only 0s and 1s.

The function should return the longest number of 1s in a row.

countZeroes([0,1,0,1,1,0,0,1]) // 2
countZeroes([1,1,1,1,0,1]) // 4

This is what I currently have.

function countZeros(arr){
  total = 0 
  for (let i=0; i < arr.length; i++){
    if (arr[i] ===1)
      total += 1
    }
    return total
  
}

I'm not sure where to go from here. I know I need to compare the right index and the left index. I tried using a nested for loop which didn't work.

Upvotes: 1

Views: 430

Answers (7)

Nathan Xabedi
Nathan Xabedi

Reputation: 1127

function countZeroes(arr) {
  let max = 0;
  let current = 0;
  for(let i=0; i < arr.length; ++i) {
    if (arr[i] == 1) {
      ++current;
    }
    else {  // arr[i] == 0
      if (current > max) max = current;
      current = 0
    }
  }
  if (current > max) max = current;
  return max;
}

Upvotes: 0

Ciabaros
Ciabaros

Reputation: 2169

Try this:

function countZeros(arr){
  let longest = 0;  // Initialize longest (0)
  let total = 0;  // Initialize total (0)
  for (let i=0; i < arr.length; i++)
  {
    if( arr[i] ===1 ) // If 1, increment total
      total += 1
    if( total > longest ) // If the total so far is longer than longest, save it.
      longest = total;
    if( arr[i] !== 1 ) // If not 1, reset total; AFTER conditionally saving it.
      total = 0;
  }
  return longest; // Return longest found
}

console.log( countZeros([0,1,0,1,1,0,0,1]) ); // 2
console.log( countZeros([1,1,1,1,0,1]) ); // 4

Upvotes: 1

Rob Monhemius
Rob Monhemius

Reputation: 5144

Here is an approach with array methods.

function count_longest_sequence_of_ones(array){
  return array
    .reduce( count_sequential_ones, [0])
    .sort()
    .pop();
  
  function count_sequential_ones(acc, number) {
    if( number === 1 ) acc[acc.length-1]++;
    else acc.push(0);
    return acc;
  }
}

console.log(count_longest_sequence_of_ones([0,1,0,1,1,0,0,1]));
console.log(count_longest_sequence_of_ones([1,1,1,1,0,1]));

PS: Looks like you got plenty of answers :). Use the one you find easiest to understand.

Upvotes: 0

SLePort
SLePort

Reputation: 15461

You can join your array and split it using 0 as separator. Resulting array of successive 1 is then sorted from shortest to longest. You can finally get the last item of array using pop():

const countZeroes = (arr) => arr.join('').split(0).sort().pop().length

console.log(countZeroes([1,0,0,1,1,1,1,0,1]))
console.log(countZeroes([0,1,0,1,1,0,0,1]))

Upvotes: 1

Sven.hig
Sven.hig

Reputation: 4519

You can use foreach and counter

g={}
function countZeroes(arr){
 var  count;
  arr.forEach((x,i)=>{
    count=g[x]||1
    if(x==1&&arr[i-1]==1) {g[x]=count+1}
  })
  g={}
  return count 
}
 console.log(countZeroes([0,1,0,1,1,0,0,1])) // 2
 console.log(countZeroes([1,1,1,1,0,1]));
console.log(countZeroes([1,1,1,1,1,1,1,0,1]));

Upvotes: 0

John
John

Reputation: 6278

This will return the longest combination of 1s.

[1,1,1,1,0,1].toString().replace(/,/gi, "").split("0").sort((a, b) => b.length - a.length)[0].length

Upvotes: 0

Majed Badawi
Majed Badawi

Reputation: 28424

You need to reset total to zero when a 0 occur and keep track of the max number of repeated 1s:

function countZeros(arr){
  total = 0, max = 0;
  for (let i=0; i < arr.length; i++){
    if (arr[i] === 1){
      total += 1;
      if(total>max)
        max = total;
    }else{
      total = 0;
    }
  }
  return max;
}

console.log(countZeros([0,1,0,1,1,0,0,1]));
console.log(countZeros([1,1,1,1,0,1]));

Upvotes: 3

Related Questions