Home > Software engineering >  Split an array when meets a certain number?
Split an array when meets a certain number?

Time:11-24

Hello I need to return count of chunks in given array of numbers.

Chunk can be defined assequence of one or more numbers separated by one or more zeroes.

Example: array [5, 4, 0, 0, -1, 0, 2, 0, 0] contains 3 chunks

so the answer should be 3 since the array can be split into three chunks.

Can you help me with the solution to this one?

Ive looped through the array but dont know how to deal with the multiple zeros.

CodePudding user response:

You could add a flag for keeping track of counting the actual chunk and rest it only by fing a zero.

let data = [5, 4, 0, 0, -1, 0, 2, 0, 0],
    count = 0,
    lastCount = false;

for (const value of data) {
    if (value === 0) {
        lastCount = false;
        continue;
    }
    if (!lastCount) {
        count  ;
        lastCount = true;
    }
}

console.log(count);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

If you needed to keep track of the chunks as well you can loop it using a simple for loop like so:

var arr = [5, 4, 0, 0, -1, 0, 2, 0, 0];

var chunks = [];

for(var i = 0; i < arr.length; i  ){
    if(arr[i] !== 0){ // check that the current number isn't a 0
        if(arr[i - 1] === 0 || arr[i - 1] === undefined){ // check if previous number was a 0
            chunks.push([]); // add a new chunk if previous number was a 0
                }

        chunks[chunks.length - 1].push(arr[i]); // push the number to latest chunk
    }
}


//console.log(chunks);
console.log("There are "   chunks.length   " chunks");
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

A simple solution leveraging the second parameter of indexOf() within a while loop.

function count_chunks(arr, delimeter = 0) {
  let count = 0, lastIndex = 0, index = 0;

  while (index !== -1 && lastIndex < arr.length) {
    index = arr.indexOf(delimeter, lastIndex);

    if (index - lastIndex) {
      count  ;
    }

    lastIndex = index   1;
  }

  return count;
}

const data = [5, 4, 0, 0, -1, 0, 2, 0, 0];
console.log(count_chunks(data));

// leading zero
console.log(count_chunks([0, 5, 4, 0, 0, -1, 0, 2]));
// no head or tail delimeters
console.log(count_chunks([5, 4, 0, 0, -1, 0, 2]));

// different delimeter
console.log(count_chunks([5, 4, 'x', 'x', -1, 'x', 2, 'x', 'x'], 'x'));
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You can solve it by using Regular expressions.

let array = [5, 4, 0, 0, -1, 0, 2, 0, 0];

let arrayToString = array.join("").toString();
let occurrenceRegex = /0 /g;  // Regex that will check for one or more occurrences

let result = arrayToString.match(occurrenceRegex).length; 

console.log(result); // Output: 3
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related