skip to Main Content

I want to push the first sequence to currentSequence, then store it in maxSequence, then push the next sequence in currentSequence, check if its length is greater, if it isn’t proceed further, if it is store it in maxSequence. I’d like to keep the original order of the array.

 let arr = ['2','1','1','2','3','3','2','2','2','1'].map(Number)

let maxSequence = []

for(let i = 0; i< arr.length; i++){
     let currentSequence = []
  if( arr[i] === arr[i+1] ){ 
    currentSequence.push(arr[i]);
  //currentSequence = arr.toSpliced(i, 1);
  }
  if(currentSequence.length > maxSequence.length){
      maxSequence = currentSequence  
  }
  }
   console.log(maxSequence.length) // expected output: 3
   console.log(maxSequence) // expected output: [2, 2, 2]
 

3

Answers


  1. This can be achieved like so:

    let arr = ['2', '1', '1', '2', '3', '3', '2', '2', '2', '1'].map(Number)
    
    let maxSequence = [];
    let currentSequence = [];
    let currentValue = -1;
    
    for (let i = 0; i < arr.length; i++) {
      if (arr[i] === currentValue) {
        currentSequence.push(arr[i]);
      } else {
        currentValue = arr[i];
        currentSequence = [currentValue];
      }
      if (currentSequence.length > maxSequence.length) {
        maxSequence = currentSequence
      }
    }
    console.log(maxSequence.length) // expected output: 3
    console.log(maxSequence) // expected output: [2, 2, 2]
    Login or Signup to reply.
  2. One way of doing what you need is:

    let arr = ["2", "1", "1", "2", "3", "3", "2", "2", "2", "1"].map(Number);
    
    let maxSequence = [];
    let currentSequence = [arr[0]];
    
    for (let i = 1; i < arr.length; i++) {
      if (arr[i] !== arr[i - 1]) { // if current number does not match the one before it
        if (currentSequence.length > maxSequence.length) { // it means currentSequence is complete and we need to check if it's larger than current maxSequence or not
          maxSequence = currentSequence;
        }
        currentSequence = [arr[i]]; // then need to reset the currentSequence array
        continue;
      }
      currentSequence.push(arr[i]); // else if numbers match, we push current number to the currentSequence
    }
    console.log(maxSequence.length); // expected output: 3
    console.log(maxSequence); // expected output: [2, 2, 2]
    Login or Signup to reply.
  3. Can be done without an intermediate array.

    let arr = ['2','1','1','2','3','3','2','2','2','1'].map(Number);
    let maxSequence;
    
    (() => {
        let maxRunVal = arr[0], maxRun = 1;
        for (let i = 1, run = 1; i < arr.length; i++) {
            if (arr[i] == arr[i-1]) {
                run++;
                if (run > maxRun) {
                    maxRun = run;
                    maxRunVal = arr[i];
                }
            }
            else {
                run = 1;
            }
        }
        maxSequence = new Array(maxRun);
        maxSequence.fill(maxRunVal);
    })();
    
    console.log(maxSequence.length);
    console.log(maxSequence);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search