My name is jeff
My name is jeff

Reputation: 69

Split an array when meets a certain number?

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.

Upvotes: 0

Views: 637

Answers (5)

vitaly-t
vitaly-t

Reputation: 25950

In case you're interested in a good-performance, concise solution, in a single iteration...

Here's one based on iter-ops library:

import {pipe, count, filter, split} from 'iter-ops';

const data = [5, 4, 0, 0, -1, 0, 2, 0, 0];

const c = pipe(
    data,
    split(v => v === 0), // split into chunks, by zero
    filter(a => !!a.length), // ignore empty chunks
    count() // count resulting chunks
).first; // take the first value (our count)

console.log(c); //=> 3

P.S. I'm the author of the library.

Upvotes: 1

Aminul Islam Alvi
Aminul Islam Alvi

Reputation: 1

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

Upvotes: 0

pilchard
pilchard

Reputation: 12957

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'));

Upvotes: 0

Adrian
Adrian

Reputation: 8607

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");

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386883

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);

Upvotes: 0

Related Questions