3

I'm trying to solve a problem that I can't seem to wrap my head around and was hoping I could get a little bit of insight from someone more experienced than I am. Basically, I have an array that has anywhere from 10 to 500 values. These values are all either true or false, but they are in a random order. I need to know the greatest amount of times that false appears in a row. For example:

[false, false, true, false, true, false, true, false, false, false]

Should return 3, since it appears 3 times in a row at the most. This seems like a potentially common problem to solve but I couldn't find a solution through searching. Any help would be greatly appreciated!

1
  • 3
    What have you tried? If you're stuck - how would you solve it by hand? That's usually a good starting point. Commented Jun 6, 2017 at 22:29

5 Answers 5

9

You can use one value to keep count of consecutive false values and if that value is larger then current max set max value to that value. If value is true you reset counter to 0.

var arr = [false, false, true, false, true, false, true, false, false, false]
var c = 0, max = 0;

arr.forEach(function(e) {
  e == false ? c++ : c = 0;
  if (c > max) max = c;
})

console.log(max)

Sign up to request clarification or add additional context in comments.

2 Comments

This makes a lot of sense, seems like the answer was right under my nose the whole time. Very much appreciated!
Glad i could help.
1

You could simply use the forEach function, and then have an internal counter, for example:

console.log( countConsecutive([false, false, true, false, true, false, true, false, false, false]) );

function countConsecutive( arr )
{
   var c = 0;
  
   arr.forEach(function(e) {  
     (e == false) ? c++ : c = 0;
  });
  
  return c;
}

Comments

1

Try this function to count number of consecutive false in array:

function countNumFalse(arr){
    var max = 0;
    var condFalse = false;
    var numFalse = 0;
    for (var i = 0; i < arr.length; i++) {
        condFalse = condFalse || arr[i];
        if(condFalse === true){
           condFalse = false;
           numFalse = 0;
        }
        else{
             numFalse++;
          if(numFalse > max){
             max = numFalse;
          }
        }
    }
    return max;
}

var arr1 = [false, false, true, false, true, false, true, false, false, false];

countNumFalse(arr1);
3

Comments

0
var yourArray=[false,true,true,false,false,false,true];
arrayLength = yourArray.length;
var maxCount=0;
var currentCount=0;
var lastElement;

for(var i = 0;i<arrayLength;i++){
    if(lastElement === yourArray[i]){
        currentCount++;
    }
    else{
        if(currentCount>maxCount){
            maxCount=currentCount;
        }
        currentCount=1;
    }
    lastElement=yourArray[i];
}
console.log(maxCount);

This should suit your needs

Comments

0

Here's a funky, but one-lined solution:

var arr = [false, false, false, true, false, false, true, false, false, true];

var consec = arr.join('').split('true').reduce((i, x) => Math.max((x.match(/false/g) || []).length, i), 0);

Steps explained:

  1. Convert the array to a string
  2. Split the new string on the values we don't want to create an array of each of the consecutive items in string form.
  3. Then reduce that array and within the reduce function, count the number of matches via a regex match - and find the maximum number of those matches.

Point of note - this really only works with binary lists [true, false], [1,0], etc.

Fiddle.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.